scan.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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 = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  823. if (!device) {
  824. printk(KERN_ERR PREFIX "Memory allocation error\n");
  825. return -ENOMEM;
  826. }
  827. device->handle = handle;
  828. device->parent = parent;
  829. acpi_device_get_busid(device, handle, type);
  830. /*
  831. * Flags
  832. * -----
  833. * Get prior to calling acpi_bus_get_status() so we know whether
  834. * or not _STA is present. Note that we only look for object
  835. * handles -- cannot evaluate objects until we know the device is
  836. * present and properly initialized.
  837. */
  838. result = acpi_bus_get_flags(device);
  839. if (result)
  840. goto end;
  841. /*
  842. * Status
  843. * ------
  844. * See if the device is present. We always assume that non-Device
  845. * and non-Processor objects (e.g. thermal zones, power resources,
  846. * etc.) are present, functioning, etc. (at least when parent object
  847. * is present). Note that _STA has a different meaning for some
  848. * objects (e.g. power resources) so we need to be careful how we use
  849. * it.
  850. */
  851. switch (type) {
  852. case ACPI_BUS_TYPE_PROCESSOR:
  853. case ACPI_BUS_TYPE_DEVICE:
  854. result = acpi_bus_get_status(device);
  855. if (ACPI_FAILURE(result) || !device->status.present) {
  856. result = -ENOENT;
  857. goto end;
  858. }
  859. break;
  860. default:
  861. STRUCT_TO_INT(device->status) = 0x0F;
  862. break;
  863. }
  864. /*
  865. * Initialize Device
  866. * -----------------
  867. * TBD: Synch with Core's enumeration/initialization process.
  868. */
  869. /*
  870. * Hardware ID, Unique ID, & Bus Address
  871. * -------------------------------------
  872. */
  873. acpi_device_set_id(device, parent, handle, type);
  874. /*
  875. * Power Management
  876. * ----------------
  877. */
  878. if (device->flags.power_manageable) {
  879. result = acpi_bus_get_power_flags(device);
  880. if (result)
  881. goto end;
  882. }
  883. /*
  884. * Wakeup device management
  885. *-----------------------
  886. */
  887. if (device->flags.wake_capable) {
  888. result = acpi_bus_get_wakeup_device_flags(device);
  889. if (result)
  890. goto end;
  891. }
  892. /*
  893. * Performance Management
  894. * ----------------------
  895. */
  896. if (device->flags.performance_manageable) {
  897. result = acpi_bus_get_perf_flags(device);
  898. if (result)
  899. goto end;
  900. }
  901. if ((result = acpi_device_set_context(device, type)))
  902. goto end;
  903. acpi_device_get_debug_info(device, handle, type);
  904. acpi_device_register(device, parent);
  905. /*
  906. * Bind _ADR-Based Devices
  907. * -----------------------
  908. * If there's a a bus address (_ADR) then we utilize the parent's
  909. * 'bind' function (if exists) to bind the ACPI- and natively-
  910. * enumerated device representations.
  911. */
  912. if (device->flags.bus_address) {
  913. if (device->parent && device->parent->ops.bind)
  914. device->parent->ops.bind(device);
  915. }
  916. /*
  917. * Locate & Attach Driver
  918. * ----------------------
  919. * If there's a hardware id (_HID) or compatible ids (_CID) we check
  920. * to see if there's a driver installed for this kind of device. Note
  921. * that drivers can install before or after a device is enumerated.
  922. *
  923. * TBD: Assumes LDM provides driver hot-plug capability.
  924. */
  925. acpi_bus_find_driver(device);
  926. end:
  927. if (!result)
  928. *child = device;
  929. else {
  930. kfree(device->pnp.cid_list);
  931. kfree(device);
  932. }
  933. return result;
  934. }
  935. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  936. {
  937. acpi_status status = AE_OK;
  938. struct acpi_device *parent = NULL;
  939. struct acpi_device *child = NULL;
  940. acpi_handle phandle = NULL;
  941. acpi_handle chandle = NULL;
  942. acpi_object_type type = 0;
  943. u32 level = 1;
  944. if (!start)
  945. return -EINVAL;
  946. parent = start;
  947. phandle = start->handle;
  948. /*
  949. * Parse through the ACPI namespace, identify all 'devices', and
  950. * create a new 'struct acpi_device' for each.
  951. */
  952. while ((level > 0) && parent) {
  953. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  954. chandle, &chandle);
  955. /*
  956. * If this scope is exhausted then move our way back up.
  957. */
  958. if (ACPI_FAILURE(status)) {
  959. level--;
  960. chandle = phandle;
  961. acpi_get_parent(phandle, &phandle);
  962. if (parent->parent)
  963. parent = parent->parent;
  964. continue;
  965. }
  966. status = acpi_get_type(chandle, &type);
  967. if (ACPI_FAILURE(status))
  968. continue;
  969. /*
  970. * If this is a scope object then parse it (depth-first).
  971. */
  972. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  973. level++;
  974. phandle = chandle;
  975. chandle = NULL;
  976. continue;
  977. }
  978. /*
  979. * We're only interested in objects that we consider 'devices'.
  980. */
  981. switch (type) {
  982. case ACPI_TYPE_DEVICE:
  983. type = ACPI_BUS_TYPE_DEVICE;
  984. break;
  985. case ACPI_TYPE_PROCESSOR:
  986. type = ACPI_BUS_TYPE_PROCESSOR;
  987. break;
  988. case ACPI_TYPE_THERMAL:
  989. type = ACPI_BUS_TYPE_THERMAL;
  990. break;
  991. case ACPI_TYPE_POWER:
  992. type = ACPI_BUS_TYPE_POWER;
  993. break;
  994. default:
  995. continue;
  996. }
  997. if (ops->acpi_op_add)
  998. status = acpi_add_single_object(&child, parent,
  999. chandle, type);
  1000. else
  1001. status = acpi_bus_get_device(chandle, &child);
  1002. if (ACPI_FAILURE(status))
  1003. continue;
  1004. if (ops->acpi_op_start) {
  1005. status = acpi_start_single_object(child);
  1006. if (ACPI_FAILURE(status))
  1007. continue;
  1008. }
  1009. /*
  1010. * If the device is present, enabled, and functioning then
  1011. * parse its scope (depth-first). Note that we need to
  1012. * represent absent devices to facilitate PnP notifications
  1013. * -- but only the subtree head (not all of its children,
  1014. * which will be enumerated when the parent is inserted).
  1015. *
  1016. * TBD: Need notifications and other detection mechanisms
  1017. * in place before we can fully implement this.
  1018. */
  1019. if (child->status.present) {
  1020. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1021. NULL, NULL);
  1022. if (ACPI_SUCCESS(status)) {
  1023. level++;
  1024. phandle = chandle;
  1025. chandle = NULL;
  1026. parent = child;
  1027. }
  1028. }
  1029. }
  1030. return 0;
  1031. }
  1032. int
  1033. acpi_bus_add(struct acpi_device **child,
  1034. struct acpi_device *parent, acpi_handle handle, int type)
  1035. {
  1036. int result;
  1037. struct acpi_bus_ops ops;
  1038. result = acpi_add_single_object(child, parent, handle, type);
  1039. if (!result) {
  1040. memset(&ops, 0, sizeof(ops));
  1041. ops.acpi_op_add = 1;
  1042. result = acpi_bus_scan(*child, &ops);
  1043. }
  1044. return result;
  1045. }
  1046. EXPORT_SYMBOL(acpi_bus_add);
  1047. int acpi_bus_start(struct acpi_device *device)
  1048. {
  1049. int result;
  1050. struct acpi_bus_ops ops;
  1051. if (!device)
  1052. return -EINVAL;
  1053. result = acpi_start_single_object(device);
  1054. if (!result) {
  1055. memset(&ops, 0, sizeof(ops));
  1056. ops.acpi_op_start = 1;
  1057. result = acpi_bus_scan(device, &ops);
  1058. }
  1059. return result;
  1060. }
  1061. EXPORT_SYMBOL(acpi_bus_start);
  1062. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1063. {
  1064. acpi_status status;
  1065. struct acpi_device *parent, *child;
  1066. acpi_handle phandle, chandle;
  1067. acpi_object_type type;
  1068. u32 level = 1;
  1069. int err = 0;
  1070. parent = start;
  1071. phandle = start->handle;
  1072. child = chandle = NULL;
  1073. while ((level > 0) && parent && (!err)) {
  1074. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1075. chandle, &chandle);
  1076. /*
  1077. * If this scope is exhausted then move our way back up.
  1078. */
  1079. if (ACPI_FAILURE(status)) {
  1080. level--;
  1081. chandle = phandle;
  1082. acpi_get_parent(phandle, &phandle);
  1083. child = parent;
  1084. parent = parent->parent;
  1085. if (level == 0)
  1086. err = acpi_bus_remove(child, rmdevice);
  1087. else
  1088. err = acpi_bus_remove(child, 1);
  1089. continue;
  1090. }
  1091. status = acpi_get_type(chandle, &type);
  1092. if (ACPI_FAILURE(status)) {
  1093. continue;
  1094. }
  1095. /*
  1096. * If there is a device corresponding to chandle then
  1097. * parse it (depth-first).
  1098. */
  1099. if (acpi_bus_get_device(chandle, &child) == 0) {
  1100. level++;
  1101. phandle = chandle;
  1102. chandle = NULL;
  1103. parent = child;
  1104. }
  1105. continue;
  1106. }
  1107. return err;
  1108. }
  1109. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1110. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1111. {
  1112. int result = 0;
  1113. struct acpi_device *device = NULL;
  1114. if (!root)
  1115. return -ENODEV;
  1116. /*
  1117. * Enumerate all fixed-feature devices.
  1118. */
  1119. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1120. result = acpi_add_single_object(&device, acpi_root,
  1121. NULL,
  1122. ACPI_BUS_TYPE_POWER_BUTTON);
  1123. if (!result)
  1124. result = acpi_start_single_object(device);
  1125. }
  1126. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1127. result = acpi_add_single_object(&device, acpi_root,
  1128. NULL,
  1129. ACPI_BUS_TYPE_SLEEP_BUTTON);
  1130. if (!result)
  1131. result = acpi_start_single_object(device);
  1132. }
  1133. return result;
  1134. }
  1135. static inline struct acpi_device * to_acpi_dev(struct device * dev)
  1136. {
  1137. return container_of(dev, struct acpi_device, dev);
  1138. }
  1139. static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
  1140. {
  1141. struct acpi_device * dev, * next;
  1142. int result;
  1143. spin_lock(&acpi_device_lock);
  1144. list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
  1145. if (dev->driver && dev->driver->ops.suspend) {
  1146. spin_unlock(&acpi_device_lock);
  1147. result = dev->driver->ops.suspend(dev, 0);
  1148. if (result) {
  1149. printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
  1150. acpi_device_name(dev),
  1151. acpi_device_bid(dev), result);
  1152. }
  1153. spin_lock(&acpi_device_lock);
  1154. }
  1155. }
  1156. spin_unlock(&acpi_device_lock);
  1157. return 0;
  1158. }
  1159. static int acpi_device_suspend(struct device * dev, pm_message_t state)
  1160. {
  1161. struct acpi_device * acpi_dev = to_acpi_dev(dev);
  1162. /*
  1163. * For now, we should only register 1 generic device -
  1164. * the ACPI root device - and from there, we walk the
  1165. * tree of ACPI devices to suspend each one using the
  1166. * ACPI driver methods.
  1167. */
  1168. if (acpi_dev->handle == ACPI_ROOT_OBJECT)
  1169. root_suspend(acpi_dev, state);
  1170. return 0;
  1171. }
  1172. static int root_resume(struct acpi_device * acpi_dev)
  1173. {
  1174. struct acpi_device * dev, * next;
  1175. int result;
  1176. spin_lock(&acpi_device_lock);
  1177. list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
  1178. if (dev->driver && dev->driver->ops.resume) {
  1179. spin_unlock(&acpi_device_lock);
  1180. result = dev->driver->ops.resume(dev, 0);
  1181. if (result) {
  1182. printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
  1183. acpi_device_name(dev),
  1184. acpi_device_bid(dev), result);
  1185. }
  1186. spin_lock(&acpi_device_lock);
  1187. }
  1188. }
  1189. spin_unlock(&acpi_device_lock);
  1190. return 0;
  1191. }
  1192. static int acpi_device_resume(struct device * dev)
  1193. {
  1194. struct acpi_device * acpi_dev = to_acpi_dev(dev);
  1195. /*
  1196. * For now, we should only register 1 generic device -
  1197. * the ACPI root device - and from there, we walk the
  1198. * tree of ACPI devices to resume each one using the
  1199. * ACPI driver methods.
  1200. */
  1201. if (acpi_dev->handle == ACPI_ROOT_OBJECT)
  1202. root_resume(acpi_dev);
  1203. return 0;
  1204. }
  1205. static struct bus_type acpi_bus_type = {
  1206. .name = "acpi",
  1207. .suspend = acpi_device_suspend,
  1208. .resume = acpi_device_resume,
  1209. };
  1210. static int __init acpi_scan_init(void)
  1211. {
  1212. int result;
  1213. struct acpi_bus_ops ops;
  1214. if (acpi_disabled)
  1215. return 0;
  1216. result = kset_register(&acpi_namespace_kset);
  1217. if (result < 0)
  1218. printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
  1219. result = bus_register(&acpi_bus_type);
  1220. if (result) {
  1221. /* We don't want to quit even if we failed to add suspend/resume */
  1222. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1223. }
  1224. /*
  1225. * Create the root device in the bus's device tree
  1226. */
  1227. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1228. ACPI_BUS_TYPE_SYSTEM);
  1229. if (result)
  1230. goto Done;
  1231. result = acpi_start_single_object(acpi_root);
  1232. if (result)
  1233. goto Done;
  1234. acpi_root->dev.bus = &acpi_bus_type;
  1235. snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
  1236. result = device_register(&acpi_root->dev);
  1237. if (result) {
  1238. /* We don't want to quit even if we failed to add suspend/resume */
  1239. printk(KERN_ERR PREFIX "Could not register device\n");
  1240. }
  1241. /*
  1242. * Enumerate devices in the ACPI namespace.
  1243. */
  1244. result = acpi_bus_scan_fixed(acpi_root);
  1245. if (!result) {
  1246. memset(&ops, 0, sizeof(ops));
  1247. ops.acpi_op_add = 1;
  1248. ops.acpi_op_start = 1;
  1249. result = acpi_bus_scan(acpi_root, &ops);
  1250. }
  1251. if (result)
  1252. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1253. Done:
  1254. return result;
  1255. }
  1256. subsys_initcall(acpi_scan_init);