scan.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. if (type == ACPI_TYPE_PROCESSOR)
  366. result = acpi_bus_trim(device, 0);
  367. else
  368. result = acpi_bus_trim(device, 1);
  369. if (!result)
  370. result = acpi_eject_operation(handle, islockable);
  371. if (result) {
  372. ret = -EBUSY;
  373. }
  374. err:
  375. return ret;
  376. }
  377. /* --------------------------------------------------------------------------
  378. Performance Management
  379. -------------------------------------------------------------------------- */
  380. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  381. {
  382. device->performance.state = ACPI_STATE_UNKNOWN;
  383. return 0;
  384. }
  385. /* --------------------------------------------------------------------------
  386. Driver Management
  387. -------------------------------------------------------------------------- */
  388. static LIST_HEAD(acpi_bus_drivers);
  389. static DECLARE_MUTEX(acpi_bus_drivers_lock);
  390. /**
  391. * acpi_bus_match - match device IDs to driver's supported IDs
  392. * @device: the device that we are trying to match to a driver
  393. * @driver: driver whose device id table is being checked
  394. *
  395. * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
  396. * matches the specified driver's criteria.
  397. */
  398. static int
  399. acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
  400. {
  401. if (driver && driver->ops.match)
  402. return driver->ops.match(device, driver);
  403. return acpi_match_ids(device, driver->ids);
  404. }
  405. /**
  406. * acpi_bus_driver_init - add a device to a driver
  407. * @device: the device to add and initialize
  408. * @driver: driver for the device
  409. *
  410. * Used to initialize a device via its device driver. Called whenever a
  411. * driver is bound to a device. Invokes the driver's add() and start() ops.
  412. */
  413. static int
  414. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  415. {
  416. int result = 0;
  417. ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
  418. if (!device || !driver)
  419. return_VALUE(-EINVAL);
  420. if (!driver->ops.add)
  421. return_VALUE(-ENOSYS);
  422. result = driver->ops.add(device);
  423. if (result) {
  424. device->driver = NULL;
  425. acpi_driver_data(device) = NULL;
  426. return_VALUE(result);
  427. }
  428. device->driver = driver;
  429. /*
  430. * TBD - Configuration Management: Assign resources to device based
  431. * upon possible configuration and currently allocated resources.
  432. */
  433. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  434. "Driver successfully bound to device\n"));
  435. return_VALUE(0);
  436. }
  437. static int acpi_start_single_object(struct acpi_device *device)
  438. {
  439. int result = 0;
  440. struct acpi_driver *driver;
  441. ACPI_FUNCTION_TRACE("acpi_start_single_object");
  442. if (!(driver = device->driver))
  443. return_VALUE(0);
  444. if (driver->ops.start) {
  445. result = driver->ops.start(device);
  446. if (result && driver->ops.remove)
  447. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  448. }
  449. return_VALUE(result);
  450. }
  451. static int acpi_driver_attach(struct acpi_driver *drv)
  452. {
  453. struct list_head *node, *next;
  454. int count = 0;
  455. ACPI_FUNCTION_TRACE("acpi_driver_attach");
  456. spin_lock(&acpi_device_lock);
  457. list_for_each_safe(node, next, &acpi_device_list) {
  458. struct acpi_device *dev =
  459. container_of(node, struct acpi_device, g_list);
  460. if (dev->driver || !dev->status.present)
  461. continue;
  462. spin_unlock(&acpi_device_lock);
  463. if (!acpi_bus_match(dev, drv)) {
  464. if (!acpi_bus_driver_init(dev, drv)) {
  465. acpi_start_single_object(dev);
  466. atomic_inc(&drv->references);
  467. count++;
  468. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  469. "Found driver [%s] for device [%s]\n",
  470. drv->name, dev->pnp.bus_id));
  471. }
  472. }
  473. spin_lock(&acpi_device_lock);
  474. }
  475. spin_unlock(&acpi_device_lock);
  476. return_VALUE(count);
  477. }
  478. static int acpi_driver_detach(struct acpi_driver *drv)
  479. {
  480. struct list_head *node, *next;
  481. ACPI_FUNCTION_TRACE("acpi_driver_detach");
  482. spin_lock(&acpi_device_lock);
  483. list_for_each_safe(node, next, &acpi_device_list) {
  484. struct acpi_device *dev =
  485. container_of(node, struct acpi_device, g_list);
  486. if (dev->driver == drv) {
  487. spin_unlock(&acpi_device_lock);
  488. if (drv->ops.remove)
  489. drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
  490. spin_lock(&acpi_device_lock);
  491. dev->driver = NULL;
  492. dev->driver_data = NULL;
  493. atomic_dec(&drv->references);
  494. }
  495. }
  496. spin_unlock(&acpi_device_lock);
  497. return_VALUE(0);
  498. }
  499. /**
  500. * acpi_bus_register_driver - register a driver with the ACPI bus
  501. * @driver: driver being registered
  502. *
  503. * Registers a driver with the ACPI bus. Searches the namespace for all
  504. * devices that match the driver's criteria and binds. Returns the
  505. * number of devices that were claimed by the driver, or a negative
  506. * error status for failure.
  507. */
  508. int acpi_bus_register_driver(struct acpi_driver *driver)
  509. {
  510. int count;
  511. ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
  512. if (acpi_disabled)
  513. return_VALUE(-ENODEV);
  514. if (!driver)
  515. return_VALUE(-EINVAL);
  516. spin_lock(&acpi_device_lock);
  517. list_add_tail(&driver->node, &acpi_bus_drivers);
  518. spin_unlock(&acpi_device_lock);
  519. count = acpi_driver_attach(driver);
  520. return_VALUE(count);
  521. }
  522. EXPORT_SYMBOL(acpi_bus_register_driver);
  523. /**
  524. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  525. * @driver: driver to unregister
  526. *
  527. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  528. * devices that match the driver's criteria and unbinds.
  529. */
  530. int acpi_bus_unregister_driver(struct acpi_driver *driver)
  531. {
  532. int error = 0;
  533. ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
  534. if (driver) {
  535. acpi_driver_detach(driver);
  536. if (!atomic_read(&driver->references)) {
  537. spin_lock(&acpi_device_lock);
  538. list_del_init(&driver->node);
  539. spin_unlock(&acpi_device_lock);
  540. }
  541. } else
  542. error = -EINVAL;
  543. return_VALUE(error);
  544. }
  545. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  546. /**
  547. * acpi_bus_find_driver - check if there is a driver installed for the device
  548. * @device: device that we are trying to find a supporting driver for
  549. *
  550. * Parses the list of registered drivers looking for a driver applicable for
  551. * the specified device.
  552. */
  553. static int acpi_bus_find_driver(struct acpi_device *device)
  554. {
  555. int result = 0;
  556. struct list_head *node, *next;
  557. ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
  558. spin_lock(&acpi_device_lock);
  559. list_for_each_safe(node, next, &acpi_bus_drivers) {
  560. struct acpi_driver *driver =
  561. container_of(node, struct acpi_driver, node);
  562. atomic_inc(&driver->references);
  563. spin_unlock(&acpi_device_lock);
  564. if (!acpi_bus_match(device, driver)) {
  565. result = acpi_bus_driver_init(device, driver);
  566. if (!result)
  567. goto Done;
  568. }
  569. atomic_dec(&driver->references);
  570. spin_lock(&acpi_device_lock);
  571. }
  572. spin_unlock(&acpi_device_lock);
  573. Done:
  574. return_VALUE(result);
  575. }
  576. /* --------------------------------------------------------------------------
  577. Device Enumeration
  578. -------------------------------------------------------------------------- */
  579. static int acpi_bus_get_flags(struct acpi_device *device)
  580. {
  581. acpi_status status = AE_OK;
  582. acpi_handle temp = NULL;
  583. ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
  584. /* Presence of _STA indicates 'dynamic_status' */
  585. status = acpi_get_handle(device->handle, "_STA", &temp);
  586. if (ACPI_SUCCESS(status))
  587. device->flags.dynamic_status = 1;
  588. /* Presence of _CID indicates 'compatible_ids' */
  589. status = acpi_get_handle(device->handle, "_CID", &temp);
  590. if (ACPI_SUCCESS(status))
  591. device->flags.compatible_ids = 1;
  592. /* Presence of _RMV indicates 'removable' */
  593. status = acpi_get_handle(device->handle, "_RMV", &temp);
  594. if (ACPI_SUCCESS(status))
  595. device->flags.removable = 1;
  596. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  597. status = acpi_get_handle(device->handle, "_EJD", &temp);
  598. if (ACPI_SUCCESS(status))
  599. device->flags.ejectable = 1;
  600. else {
  601. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  602. if (ACPI_SUCCESS(status))
  603. device->flags.ejectable = 1;
  604. }
  605. /* Presence of _LCK indicates 'lockable' */
  606. status = acpi_get_handle(device->handle, "_LCK", &temp);
  607. if (ACPI_SUCCESS(status))
  608. device->flags.lockable = 1;
  609. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  610. status = acpi_get_handle(device->handle, "_PS0", &temp);
  611. if (ACPI_FAILURE(status))
  612. status = acpi_get_handle(device->handle, "_PR0", &temp);
  613. if (ACPI_SUCCESS(status))
  614. device->flags.power_manageable = 1;
  615. /* Presence of _PRW indicates wake capable */
  616. status = acpi_get_handle(device->handle, "_PRW", &temp);
  617. if (ACPI_SUCCESS(status))
  618. device->flags.wake_capable = 1;
  619. /* TBD: Peformance management */
  620. return_VALUE(0);
  621. }
  622. static void acpi_device_get_busid(struct acpi_device *device,
  623. acpi_handle handle, int type)
  624. {
  625. char bus_id[5] = { '?', 0 };
  626. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  627. int i = 0;
  628. /*
  629. * Bus ID
  630. * ------
  631. * The device's Bus ID is simply the object name.
  632. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  633. */
  634. switch (type) {
  635. case ACPI_BUS_TYPE_SYSTEM:
  636. strcpy(device->pnp.bus_id, "ACPI");
  637. break;
  638. case ACPI_BUS_TYPE_POWER_BUTTON:
  639. strcpy(device->pnp.bus_id, "PWRF");
  640. break;
  641. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  642. strcpy(device->pnp.bus_id, "SLPF");
  643. break;
  644. default:
  645. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  646. /* Clean up trailing underscores (if any) */
  647. for (i = 3; i > 1; i--) {
  648. if (bus_id[i] == '_')
  649. bus_id[i] = '\0';
  650. else
  651. break;
  652. }
  653. strcpy(device->pnp.bus_id, bus_id);
  654. break;
  655. }
  656. }
  657. static void acpi_device_set_id(struct acpi_device *device,
  658. struct acpi_device *parent, acpi_handle handle,
  659. int type)
  660. {
  661. struct acpi_device_info *info;
  662. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  663. char *hid = NULL;
  664. char *uid = NULL;
  665. struct acpi_compatible_id_list *cid_list = NULL;
  666. acpi_status status;
  667. switch (type) {
  668. case ACPI_BUS_TYPE_DEVICE:
  669. status = acpi_get_object_info(handle, &buffer);
  670. if (ACPI_FAILURE(status)) {
  671. printk("%s: Error reading device info\n", __FUNCTION__);
  672. return;
  673. }
  674. info = buffer.pointer;
  675. if (info->valid & ACPI_VALID_HID)
  676. hid = info->hardware_id.value;
  677. if (info->valid & ACPI_VALID_UID)
  678. uid = info->unique_id.value;
  679. if (info->valid & ACPI_VALID_CID)
  680. cid_list = &info->compatibility_id;
  681. if (info->valid & ACPI_VALID_ADR) {
  682. device->pnp.bus_address = info->address;
  683. device->flags.bus_address = 1;
  684. }
  685. break;
  686. case ACPI_BUS_TYPE_POWER:
  687. hid = ACPI_POWER_HID;
  688. break;
  689. case ACPI_BUS_TYPE_PROCESSOR:
  690. hid = ACPI_PROCESSOR_HID;
  691. break;
  692. case ACPI_BUS_TYPE_SYSTEM:
  693. hid = ACPI_SYSTEM_HID;
  694. break;
  695. case ACPI_BUS_TYPE_THERMAL:
  696. hid = ACPI_THERMAL_HID;
  697. break;
  698. case ACPI_BUS_TYPE_POWER_BUTTON:
  699. hid = ACPI_BUTTON_HID_POWERF;
  700. break;
  701. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  702. hid = ACPI_BUTTON_HID_SLEEPF;
  703. break;
  704. }
  705. /*
  706. * \_SB
  707. * ----
  708. * Fix for the system root bus device -- the only root-level device.
  709. */
  710. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  711. hid = ACPI_BUS_HID;
  712. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  713. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  714. }
  715. if (hid) {
  716. strcpy(device->pnp.hardware_id, hid);
  717. device->flags.hardware_id = 1;
  718. }
  719. if (uid) {
  720. strcpy(device->pnp.unique_id, uid);
  721. device->flags.unique_id = 1;
  722. }
  723. if (cid_list) {
  724. device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
  725. if (device->pnp.cid_list)
  726. memcpy(device->pnp.cid_list, cid_list, cid_list->size);
  727. else
  728. printk(KERN_ERR "Memory allocation error\n");
  729. }
  730. acpi_os_free(buffer.pointer);
  731. }
  732. static int acpi_device_set_context(struct acpi_device *device, int type)
  733. {
  734. acpi_status status = AE_OK;
  735. int result = 0;
  736. /*
  737. * Context
  738. * -------
  739. * Attach this 'struct acpi_device' to the ACPI object. This makes
  740. * resolutions from handle->device very efficient. Note that we need
  741. * to be careful with fixed-feature devices as they all attach to the
  742. * root object.
  743. */
  744. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  745. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  746. status = acpi_attach_data(device->handle,
  747. acpi_bus_data_handler, device);
  748. if (ACPI_FAILURE(status)) {
  749. printk("Error attaching device data\n");
  750. result = -ENODEV;
  751. }
  752. }
  753. return result;
  754. }
  755. static void acpi_device_get_debug_info(struct acpi_device *device,
  756. acpi_handle handle, int type)
  757. {
  758. #ifdef CONFIG_ACPI_DEBUG_OUTPUT
  759. char *type_string = NULL;
  760. char name[80] = { '?', '\0' };
  761. struct acpi_buffer buffer = { sizeof(name), name };
  762. switch (type) {
  763. case ACPI_BUS_TYPE_DEVICE:
  764. type_string = "Device";
  765. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  766. break;
  767. case ACPI_BUS_TYPE_POWER:
  768. type_string = "Power Resource";
  769. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  770. break;
  771. case ACPI_BUS_TYPE_PROCESSOR:
  772. type_string = "Processor";
  773. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  774. break;
  775. case ACPI_BUS_TYPE_SYSTEM:
  776. type_string = "System";
  777. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  778. break;
  779. case ACPI_BUS_TYPE_THERMAL:
  780. type_string = "Thermal Zone";
  781. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  782. break;
  783. case ACPI_BUS_TYPE_POWER_BUTTON:
  784. type_string = "Power Button";
  785. sprintf(name, "PWRB");
  786. break;
  787. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  788. type_string = "Sleep Button";
  789. sprintf(name, "SLPB");
  790. break;
  791. }
  792. printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
  793. #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
  794. }
  795. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  796. {
  797. int result = 0;
  798. struct acpi_driver *driver;
  799. ACPI_FUNCTION_TRACE("acpi_bus_remove");
  800. if (!dev)
  801. return_VALUE(-EINVAL);
  802. driver = dev->driver;
  803. if ((driver) && (driver->ops.remove)) {
  804. if (driver->ops.stop) {
  805. result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
  806. if (result)
  807. return_VALUE(result);
  808. }
  809. result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
  810. if (result) {
  811. return_VALUE(result);
  812. }
  813. atomic_dec(&dev->driver->references);
  814. dev->driver = NULL;
  815. acpi_driver_data(dev) = NULL;
  816. }
  817. if (!rmdevice)
  818. return_VALUE(0);
  819. if (dev->flags.bus_address) {
  820. if ((dev->parent) && (dev->parent->ops.unbind))
  821. dev->parent->ops.unbind(dev);
  822. }
  823. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  824. return_VALUE(0);
  825. }
  826. static int
  827. acpi_add_single_object(struct acpi_device **child,
  828. struct acpi_device *parent, acpi_handle handle, int type)
  829. {
  830. int result = 0;
  831. struct acpi_device *device = NULL;
  832. ACPI_FUNCTION_TRACE("acpi_add_single_object");
  833. if (!child)
  834. return_VALUE(-EINVAL);
  835. device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
  836. if (!device) {
  837. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
  838. return_VALUE(-ENOMEM);
  839. }
  840. memset(device, 0, sizeof(struct acpi_device));
  841. device->handle = handle;
  842. device->parent = parent;
  843. acpi_device_get_busid(device, handle, type);
  844. /*
  845. * Flags
  846. * -----
  847. * Get prior to calling acpi_bus_get_status() so we know whether
  848. * or not _STA is present. Note that we only look for object
  849. * handles -- cannot evaluate objects until we know the device is
  850. * present and properly initialized.
  851. */
  852. result = acpi_bus_get_flags(device);
  853. if (result)
  854. goto end;
  855. /*
  856. * Status
  857. * ------
  858. * See if the device is present. We always assume that non-Device
  859. * and non-Processor objects (e.g. thermal zones, power resources,
  860. * etc.) are present, functioning, etc. (at least when parent object
  861. * is present). Note that _STA has a different meaning for some
  862. * objects (e.g. power resources) so we need to be careful how we use
  863. * it.
  864. */
  865. switch (type) {
  866. case ACPI_BUS_TYPE_PROCESSOR:
  867. case ACPI_BUS_TYPE_DEVICE:
  868. result = acpi_bus_get_status(device);
  869. if (ACPI_FAILURE(result) || !device->status.present) {
  870. result = -ENOENT;
  871. goto end;
  872. }
  873. break;
  874. default:
  875. STRUCT_TO_INT(device->status) = 0x0F;
  876. break;
  877. }
  878. /*
  879. * Initialize Device
  880. * -----------------
  881. * TBD: Synch with Core's enumeration/initialization process.
  882. */
  883. /*
  884. * Hardware ID, Unique ID, & Bus Address
  885. * -------------------------------------
  886. */
  887. acpi_device_set_id(device, parent, handle, type);
  888. /*
  889. * Power Management
  890. * ----------------
  891. */
  892. if (device->flags.power_manageable) {
  893. result = acpi_bus_get_power_flags(device);
  894. if (result)
  895. goto end;
  896. }
  897. /*
  898. * Wakeup device management
  899. *-----------------------
  900. */
  901. if (device->flags.wake_capable) {
  902. result = acpi_bus_get_wakeup_device_flags(device);
  903. if (result)
  904. goto end;
  905. }
  906. /*
  907. * Performance Management
  908. * ----------------------
  909. */
  910. if (device->flags.performance_manageable) {
  911. result = acpi_bus_get_perf_flags(device);
  912. if (result)
  913. goto end;
  914. }
  915. if ((result = acpi_device_set_context(device, type)))
  916. goto end;
  917. acpi_device_get_debug_info(device, handle, type);
  918. acpi_device_register(device, parent);
  919. /*
  920. * Bind _ADR-Based Devices
  921. * -----------------------
  922. * If there's a a bus address (_ADR) then we utilize the parent's
  923. * 'bind' function (if exists) to bind the ACPI- and natively-
  924. * enumerated device representations.
  925. */
  926. if (device->flags.bus_address) {
  927. if (device->parent && device->parent->ops.bind)
  928. device->parent->ops.bind(device);
  929. }
  930. /*
  931. * Locate & Attach Driver
  932. * ----------------------
  933. * If there's a hardware id (_HID) or compatible ids (_CID) we check
  934. * to see if there's a driver installed for this kind of device. Note
  935. * that drivers can install before or after a device is enumerated.
  936. *
  937. * TBD: Assumes LDM provides driver hot-plug capability.
  938. */
  939. acpi_bus_find_driver(device);
  940. end:
  941. if (!result)
  942. *child = device;
  943. else {
  944. kfree(device->pnp.cid_list);
  945. kfree(device);
  946. }
  947. return_VALUE(result);
  948. }
  949. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  950. {
  951. acpi_status status = AE_OK;
  952. struct acpi_device *parent = NULL;
  953. struct acpi_device *child = NULL;
  954. acpi_handle phandle = NULL;
  955. acpi_handle chandle = NULL;
  956. acpi_object_type type = 0;
  957. u32 level = 1;
  958. ACPI_FUNCTION_TRACE("acpi_bus_scan");
  959. if (!start)
  960. return_VALUE(-EINVAL);
  961. parent = start;
  962. phandle = start->handle;
  963. /*
  964. * Parse through the ACPI namespace, identify all 'devices', and
  965. * create a new 'struct acpi_device' for each.
  966. */
  967. while ((level > 0) && parent) {
  968. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  969. chandle, &chandle);
  970. /*
  971. * If this scope is exhausted then move our way back up.
  972. */
  973. if (ACPI_FAILURE(status)) {
  974. level--;
  975. chandle = phandle;
  976. acpi_get_parent(phandle, &phandle);
  977. if (parent->parent)
  978. parent = parent->parent;
  979. continue;
  980. }
  981. status = acpi_get_type(chandle, &type);
  982. if (ACPI_FAILURE(status))
  983. continue;
  984. /*
  985. * If this is a scope object then parse it (depth-first).
  986. */
  987. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  988. level++;
  989. phandle = chandle;
  990. chandle = NULL;
  991. continue;
  992. }
  993. /*
  994. * We're only interested in objects that we consider 'devices'.
  995. */
  996. switch (type) {
  997. case ACPI_TYPE_DEVICE:
  998. type = ACPI_BUS_TYPE_DEVICE;
  999. break;
  1000. case ACPI_TYPE_PROCESSOR:
  1001. type = ACPI_BUS_TYPE_PROCESSOR;
  1002. break;
  1003. case ACPI_TYPE_THERMAL:
  1004. type = ACPI_BUS_TYPE_THERMAL;
  1005. break;
  1006. case ACPI_TYPE_POWER:
  1007. type = ACPI_BUS_TYPE_POWER;
  1008. break;
  1009. default:
  1010. continue;
  1011. }
  1012. if (ops->acpi_op_add)
  1013. status = acpi_add_single_object(&child, parent,
  1014. chandle, type);
  1015. else
  1016. status = acpi_bus_get_device(chandle, &child);
  1017. if (ACPI_FAILURE(status))
  1018. continue;
  1019. if (ops->acpi_op_start) {
  1020. status = acpi_start_single_object(child);
  1021. if (ACPI_FAILURE(status))
  1022. continue;
  1023. }
  1024. /*
  1025. * If the device is present, enabled, and functioning then
  1026. * parse its scope (depth-first). Note that we need to
  1027. * represent absent devices to facilitate PnP notifications
  1028. * -- but only the subtree head (not all of its children,
  1029. * which will be enumerated when the parent is inserted).
  1030. *
  1031. * TBD: Need notifications and other detection mechanisms
  1032. * in place before we can fully implement this.
  1033. */
  1034. if (child->status.present) {
  1035. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1036. NULL, NULL);
  1037. if (ACPI_SUCCESS(status)) {
  1038. level++;
  1039. phandle = chandle;
  1040. chandle = NULL;
  1041. parent = child;
  1042. }
  1043. }
  1044. }
  1045. return_VALUE(0);
  1046. }
  1047. int
  1048. acpi_bus_add(struct acpi_device **child,
  1049. struct acpi_device *parent, acpi_handle handle, int type)
  1050. {
  1051. int result;
  1052. struct acpi_bus_ops ops;
  1053. ACPI_FUNCTION_TRACE("acpi_bus_add");
  1054. result = acpi_add_single_object(child, parent, handle, type);
  1055. if (!result) {
  1056. memset(&ops, 0, sizeof(ops));
  1057. ops.acpi_op_add = 1;
  1058. result = acpi_bus_scan(*child, &ops);
  1059. }
  1060. return_VALUE(result);
  1061. }
  1062. EXPORT_SYMBOL(acpi_bus_add);
  1063. int acpi_bus_start(struct acpi_device *device)
  1064. {
  1065. int result;
  1066. struct acpi_bus_ops ops;
  1067. ACPI_FUNCTION_TRACE("acpi_bus_start");
  1068. if (!device)
  1069. return_VALUE(-EINVAL);
  1070. result = acpi_start_single_object(device);
  1071. if (!result) {
  1072. memset(&ops, 0, sizeof(ops));
  1073. ops.acpi_op_start = 1;
  1074. result = acpi_bus_scan(device, &ops);
  1075. }
  1076. return_VALUE(result);
  1077. }
  1078. EXPORT_SYMBOL(acpi_bus_start);
  1079. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1080. {
  1081. acpi_status status;
  1082. struct acpi_device *parent, *child;
  1083. acpi_handle phandle, chandle;
  1084. acpi_object_type type;
  1085. u32 level = 1;
  1086. int err = 0;
  1087. parent = start;
  1088. phandle = start->handle;
  1089. child = chandle = NULL;
  1090. while ((level > 0) && parent && (!err)) {
  1091. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1092. chandle, &chandle);
  1093. /*
  1094. * If this scope is exhausted then move our way back up.
  1095. */
  1096. if (ACPI_FAILURE(status)) {
  1097. level--;
  1098. chandle = phandle;
  1099. acpi_get_parent(phandle, &phandle);
  1100. child = parent;
  1101. parent = parent->parent;
  1102. if (level == 0)
  1103. err = acpi_bus_remove(child, rmdevice);
  1104. else
  1105. err = acpi_bus_remove(child, 1);
  1106. continue;
  1107. }
  1108. status = acpi_get_type(chandle, &type);
  1109. if (ACPI_FAILURE(status)) {
  1110. continue;
  1111. }
  1112. /*
  1113. * If there is a device corresponding to chandle then
  1114. * parse it (depth-first).
  1115. */
  1116. if (acpi_bus_get_device(chandle, &child) == 0) {
  1117. level++;
  1118. phandle = chandle;
  1119. chandle = NULL;
  1120. parent = child;
  1121. }
  1122. continue;
  1123. }
  1124. return err;
  1125. }
  1126. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1127. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1128. {
  1129. int result = 0;
  1130. struct acpi_device *device = NULL;
  1131. ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
  1132. if (!root)
  1133. return_VALUE(-ENODEV);
  1134. /*
  1135. * Enumerate all fixed-feature devices.
  1136. */
  1137. if (acpi_fadt.pwr_button == 0) {
  1138. result = acpi_add_single_object(&device, acpi_root,
  1139. NULL,
  1140. ACPI_BUS_TYPE_POWER_BUTTON);
  1141. if (!result)
  1142. result = acpi_start_single_object(device);
  1143. }
  1144. if (acpi_fadt.sleep_button == 0) {
  1145. result = acpi_add_single_object(&device, acpi_root,
  1146. NULL,
  1147. ACPI_BUS_TYPE_SLEEP_BUTTON);
  1148. if (!result)
  1149. result = acpi_start_single_object(device);
  1150. }
  1151. return_VALUE(result);
  1152. }
  1153. static int __init acpi_scan_init(void)
  1154. {
  1155. int result;
  1156. struct acpi_bus_ops ops;
  1157. ACPI_FUNCTION_TRACE("acpi_scan_init");
  1158. if (acpi_disabled)
  1159. return_VALUE(0);
  1160. kset_register(&acpi_namespace_kset);
  1161. /*
  1162. * Create the root device in the bus's device tree
  1163. */
  1164. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1165. ACPI_BUS_TYPE_SYSTEM);
  1166. if (result)
  1167. goto Done;
  1168. result = acpi_start_single_object(acpi_root);
  1169. /*
  1170. * Enumerate devices in the ACPI namespace.
  1171. */
  1172. result = acpi_bus_scan_fixed(acpi_root);
  1173. if (!result) {
  1174. memset(&ops, 0, sizeof(ops));
  1175. ops.acpi_op_add = 1;
  1176. ops.acpi_op_start = 1;
  1177. result = acpi_bus_scan(acpi_root, &ops);
  1178. }
  1179. if (result)
  1180. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1181. Done:
  1182. return_VALUE(result);
  1183. }
  1184. subsys_initcall(acpi_scan_init);