scan.c 34 KB

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