scan.c 36 KB

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