scan.c 36 KB

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