scan.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 <linux/signal.h>
  9. #include <linux/kthread.h>
  10. #include <acpi/acpi_drivers.h>
  11. #include "internal.h"
  12. #define _COMPONENT ACPI_BUS_COMPONENT
  13. ACPI_MODULE_NAME("scan");
  14. #define STRUCT_TO_INT(s) (*((int*)&s))
  15. extern struct acpi_device *acpi_root;
  16. #define ACPI_BUS_CLASS "system_bus"
  17. #define ACPI_BUS_HID "LNXSYBUS"
  18. #define ACPI_BUS_DEVICE_NAME "System Bus"
  19. #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
  20. static LIST_HEAD(acpi_device_list);
  21. static LIST_HEAD(acpi_bus_id_list);
  22. DEFINE_MUTEX(acpi_device_lock);
  23. LIST_HEAD(acpi_wakeup_device_list);
  24. struct acpi_device_bus_id{
  25. char bus_id[15];
  26. unsigned int instance_no;
  27. struct list_head node;
  28. };
  29. /*
  30. * Creates hid/cid(s) string needed for modalias and uevent
  31. * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  32. * char *modalias: "acpi:IBM0001:ACPI0001"
  33. */
  34. static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
  35. int size)
  36. {
  37. int len;
  38. int count;
  39. struct acpi_hardware_id *id;
  40. len = snprintf(modalias, size, "acpi:");
  41. size -= len;
  42. list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
  43. count = snprintf(&modalias[len], size, "%s:", id->id);
  44. if (count < 0 || count >= size)
  45. return -EINVAL;
  46. len += count;
  47. size -= count;
  48. }
  49. modalias[len] = '\0';
  50. return len;
  51. }
  52. static ssize_t
  53. acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
  54. struct acpi_device *acpi_dev = to_acpi_device(dev);
  55. int len;
  56. /* Device has no HID and no CID or string is >1024 */
  57. len = create_modalias(acpi_dev, buf, 1024);
  58. if (len <= 0)
  59. return 0;
  60. buf[len++] = '\n';
  61. return len;
  62. }
  63. static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
  64. static void acpi_bus_hot_remove_device(void *context)
  65. {
  66. struct acpi_device *device;
  67. acpi_handle handle = context;
  68. struct acpi_object_list arg_list;
  69. union acpi_object arg;
  70. acpi_status status = AE_OK;
  71. if (acpi_bus_get_device(handle, &device))
  72. return;
  73. if (!device)
  74. return;
  75. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  76. "Hot-removing device %s...\n", dev_name(&device->dev)));
  77. if (acpi_bus_trim(device, 1)) {
  78. printk(KERN_ERR PREFIX
  79. "Removing device failed\n");
  80. return;
  81. }
  82. /* power off device */
  83. status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
  84. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  85. printk(KERN_WARNING PREFIX
  86. "Power-off device failed\n");
  87. if (device->flags.lockable) {
  88. arg_list.count = 1;
  89. arg_list.pointer = &arg;
  90. arg.type = ACPI_TYPE_INTEGER;
  91. arg.integer.value = 0;
  92. acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
  93. }
  94. arg_list.count = 1;
  95. arg_list.pointer = &arg;
  96. arg.type = ACPI_TYPE_INTEGER;
  97. arg.integer.value = 1;
  98. /*
  99. * TBD: _EJD support.
  100. */
  101. status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
  102. if (ACPI_FAILURE(status))
  103. printk(KERN_WARNING PREFIX
  104. "Eject device failed\n");
  105. return;
  106. }
  107. static ssize_t
  108. acpi_eject_store(struct device *d, struct device_attribute *attr,
  109. const char *buf, size_t count)
  110. {
  111. int ret = count;
  112. acpi_status status;
  113. acpi_object_type type = 0;
  114. struct acpi_device *acpi_device = to_acpi_device(d);
  115. if ((!count) || (buf[0] != '1')) {
  116. return -EINVAL;
  117. }
  118. #ifndef FORCE_EJECT
  119. if (acpi_device->driver == NULL) {
  120. ret = -ENODEV;
  121. goto err;
  122. }
  123. #endif
  124. status = acpi_get_type(acpi_device->handle, &type);
  125. if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
  126. ret = -ENODEV;
  127. goto err;
  128. }
  129. acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
  130. err:
  131. return ret;
  132. }
  133. static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
  134. static ssize_t
  135. acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
  136. struct acpi_device *acpi_dev = to_acpi_device(dev);
  137. return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
  138. }
  139. static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
  140. static ssize_t
  141. acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
  142. struct acpi_device *acpi_dev = to_acpi_device(dev);
  143. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  144. int result;
  145. result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
  146. if (result)
  147. goto end;
  148. result = sprintf(buf, "%s\n", (char*)path.pointer);
  149. kfree(path.pointer);
  150. end:
  151. return result;
  152. }
  153. static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
  154. static int acpi_device_setup_files(struct acpi_device *dev)
  155. {
  156. acpi_status status;
  157. acpi_handle temp;
  158. int result = 0;
  159. /*
  160. * Devices gotten from FADT don't have a "path" attribute
  161. */
  162. if (dev->handle) {
  163. result = device_create_file(&dev->dev, &dev_attr_path);
  164. if (result)
  165. goto end;
  166. }
  167. result = device_create_file(&dev->dev, &dev_attr_hid);
  168. if (result)
  169. goto end;
  170. result = device_create_file(&dev->dev, &dev_attr_modalias);
  171. if (result)
  172. goto end;
  173. /*
  174. * If device has _EJ0, 'eject' file is created that is used to trigger
  175. * hot-removal function from userland.
  176. */
  177. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  178. if (ACPI_SUCCESS(status))
  179. result = device_create_file(&dev->dev, &dev_attr_eject);
  180. end:
  181. return result;
  182. }
  183. static void acpi_device_remove_files(struct acpi_device *dev)
  184. {
  185. acpi_status status;
  186. acpi_handle temp;
  187. /*
  188. * If device has _EJ0, 'eject' file is created that is used to trigger
  189. * hot-removal function from userland.
  190. */
  191. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  192. if (ACPI_SUCCESS(status))
  193. device_remove_file(&dev->dev, &dev_attr_eject);
  194. device_remove_file(&dev->dev, &dev_attr_modalias);
  195. device_remove_file(&dev->dev, &dev_attr_hid);
  196. if (dev->handle)
  197. device_remove_file(&dev->dev, &dev_attr_path);
  198. }
  199. /* --------------------------------------------------------------------------
  200. ACPI Bus operations
  201. -------------------------------------------------------------------------- */
  202. int acpi_match_device_ids(struct acpi_device *device,
  203. const struct acpi_device_id *ids)
  204. {
  205. const struct acpi_device_id *id;
  206. struct acpi_hardware_id *hwid;
  207. /*
  208. * If the device is not present, it is unnecessary to load device
  209. * driver for it.
  210. */
  211. if (!device->status.present)
  212. return -ENODEV;
  213. for (id = ids; id->id[0]; id++)
  214. list_for_each_entry(hwid, &device->pnp.ids, list)
  215. if (!strcmp((char *) id->id, hwid->id))
  216. return 0;
  217. return -ENOENT;
  218. }
  219. EXPORT_SYMBOL(acpi_match_device_ids);
  220. static void acpi_free_ids(struct acpi_device *device)
  221. {
  222. struct acpi_hardware_id *id, *tmp;
  223. list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
  224. kfree(id->id);
  225. kfree(id);
  226. }
  227. }
  228. static void acpi_device_release(struct device *dev)
  229. {
  230. struct acpi_device *acpi_dev = to_acpi_device(dev);
  231. acpi_free_ids(acpi_dev);
  232. kfree(acpi_dev);
  233. }
  234. static int acpi_device_suspend(struct device *dev, pm_message_t state)
  235. {
  236. struct acpi_device *acpi_dev = to_acpi_device(dev);
  237. struct acpi_driver *acpi_drv = acpi_dev->driver;
  238. if (acpi_drv && acpi_drv->ops.suspend)
  239. return acpi_drv->ops.suspend(acpi_dev, state);
  240. return 0;
  241. }
  242. static int acpi_device_resume(struct device *dev)
  243. {
  244. struct acpi_device *acpi_dev = to_acpi_device(dev);
  245. struct acpi_driver *acpi_drv = acpi_dev->driver;
  246. if (acpi_drv && acpi_drv->ops.resume)
  247. return acpi_drv->ops.resume(acpi_dev);
  248. return 0;
  249. }
  250. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  251. {
  252. struct acpi_device *acpi_dev = to_acpi_device(dev);
  253. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  254. return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  255. }
  256. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  257. {
  258. struct acpi_device *acpi_dev = to_acpi_device(dev);
  259. int len;
  260. if (add_uevent_var(env, "MODALIAS="))
  261. return -ENOMEM;
  262. len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
  263. sizeof(env->buf) - env->buflen);
  264. if (len >= (sizeof(env->buf) - env->buflen))
  265. return -ENOMEM;
  266. env->buflen += len;
  267. return 0;
  268. }
  269. static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
  270. {
  271. struct acpi_device *device = data;
  272. device->driver->ops.notify(device, event);
  273. }
  274. static acpi_status acpi_device_notify_fixed(void *data)
  275. {
  276. struct acpi_device *device = data;
  277. /* Fixed hardware devices have no handles */
  278. acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
  279. return AE_OK;
  280. }
  281. static int acpi_device_install_notify_handler(struct acpi_device *device)
  282. {
  283. acpi_status status;
  284. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  285. status =
  286. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  287. acpi_device_notify_fixed,
  288. device);
  289. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  290. status =
  291. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  292. acpi_device_notify_fixed,
  293. device);
  294. else
  295. status = acpi_install_notify_handler(device->handle,
  296. ACPI_DEVICE_NOTIFY,
  297. acpi_device_notify,
  298. device);
  299. if (ACPI_FAILURE(status))
  300. return -EINVAL;
  301. return 0;
  302. }
  303. static void acpi_device_remove_notify_handler(struct acpi_device *device)
  304. {
  305. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  306. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  307. acpi_device_notify_fixed);
  308. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  309. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  310. acpi_device_notify_fixed);
  311. else
  312. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  313. acpi_device_notify);
  314. }
  315. static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
  316. static int acpi_start_single_object(struct acpi_device *);
  317. static int acpi_device_probe(struct device * dev)
  318. {
  319. struct acpi_device *acpi_dev = to_acpi_device(dev);
  320. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  321. int ret;
  322. ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
  323. if (!ret) {
  324. if (acpi_dev->bus_ops.acpi_op_start)
  325. acpi_start_single_object(acpi_dev);
  326. if (acpi_drv->ops.notify) {
  327. ret = acpi_device_install_notify_handler(acpi_dev);
  328. if (ret) {
  329. if (acpi_drv->ops.remove)
  330. acpi_drv->ops.remove(acpi_dev,
  331. acpi_dev->removal_type);
  332. return ret;
  333. }
  334. }
  335. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  336. "Found driver [%s] for device [%s]\n",
  337. acpi_drv->name, acpi_dev->pnp.bus_id));
  338. get_device(dev);
  339. }
  340. return ret;
  341. }
  342. static int acpi_device_remove(struct device * dev)
  343. {
  344. struct acpi_device *acpi_dev = to_acpi_device(dev);
  345. struct acpi_driver *acpi_drv = acpi_dev->driver;
  346. if (acpi_drv) {
  347. if (acpi_drv->ops.notify)
  348. acpi_device_remove_notify_handler(acpi_dev);
  349. if (acpi_drv->ops.remove)
  350. acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
  351. }
  352. acpi_dev->driver = NULL;
  353. acpi_dev->driver_data = NULL;
  354. put_device(dev);
  355. return 0;
  356. }
  357. struct bus_type acpi_bus_type = {
  358. .name = "acpi",
  359. .suspend = acpi_device_suspend,
  360. .resume = acpi_device_resume,
  361. .match = acpi_bus_match,
  362. .probe = acpi_device_probe,
  363. .remove = acpi_device_remove,
  364. .uevent = acpi_device_uevent,
  365. };
  366. static int acpi_device_register(struct acpi_device *device)
  367. {
  368. int result;
  369. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  370. int found = 0;
  371. /*
  372. * Linkage
  373. * -------
  374. * Link this device to its parent and siblings.
  375. */
  376. INIT_LIST_HEAD(&device->children);
  377. INIT_LIST_HEAD(&device->node);
  378. INIT_LIST_HEAD(&device->wakeup_list);
  379. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  380. if (!new_bus_id) {
  381. printk(KERN_ERR PREFIX "Memory allocation error\n");
  382. return -ENOMEM;
  383. }
  384. mutex_lock(&acpi_device_lock);
  385. /*
  386. * Find suitable bus_id and instance number in acpi_bus_id_list
  387. * If failed, create one and link it into acpi_bus_id_list
  388. */
  389. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  390. if (!strcmp(acpi_device_bus_id->bus_id,
  391. acpi_device_hid(device))) {
  392. acpi_device_bus_id->instance_no++;
  393. found = 1;
  394. kfree(new_bus_id);
  395. break;
  396. }
  397. }
  398. if (!found) {
  399. acpi_device_bus_id = new_bus_id;
  400. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  401. acpi_device_bus_id->instance_no = 0;
  402. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  403. }
  404. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  405. if (device->parent)
  406. list_add_tail(&device->node, &device->parent->children);
  407. if (device->wakeup.flags.valid)
  408. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  409. mutex_unlock(&acpi_device_lock);
  410. if (device->parent)
  411. device->dev.parent = &device->parent->dev;
  412. device->dev.bus = &acpi_bus_type;
  413. device->dev.release = &acpi_device_release;
  414. result = device_register(&device->dev);
  415. if (result) {
  416. dev_err(&device->dev, "Error registering device\n");
  417. goto end;
  418. }
  419. result = acpi_device_setup_files(device);
  420. if (result)
  421. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  422. dev_name(&device->dev));
  423. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  424. return 0;
  425. end:
  426. mutex_lock(&acpi_device_lock);
  427. if (device->parent)
  428. list_del(&device->node);
  429. list_del(&device->wakeup_list);
  430. mutex_unlock(&acpi_device_lock);
  431. return result;
  432. }
  433. static void acpi_device_unregister(struct acpi_device *device, int type)
  434. {
  435. mutex_lock(&acpi_device_lock);
  436. if (device->parent)
  437. list_del(&device->node);
  438. list_del(&device->wakeup_list);
  439. mutex_unlock(&acpi_device_lock);
  440. acpi_detach_data(device->handle, acpi_bus_data_handler);
  441. acpi_device_remove_files(device);
  442. device_unregister(&device->dev);
  443. }
  444. /* --------------------------------------------------------------------------
  445. Driver Management
  446. -------------------------------------------------------------------------- */
  447. /**
  448. * acpi_bus_driver_init - add a device to a driver
  449. * @device: the device to add and initialize
  450. * @driver: driver for the device
  451. *
  452. * Used to initialize a device via its device driver. Called whenever a
  453. * driver is bound to a device. Invokes the driver's add() ops.
  454. */
  455. static int
  456. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  457. {
  458. int result = 0;
  459. if (!device || !driver)
  460. return -EINVAL;
  461. if (!driver->ops.add)
  462. return -ENOSYS;
  463. result = driver->ops.add(device);
  464. if (result) {
  465. device->driver = NULL;
  466. device->driver_data = NULL;
  467. return result;
  468. }
  469. device->driver = driver;
  470. /*
  471. * TBD - Configuration Management: Assign resources to device based
  472. * upon possible configuration and currently allocated resources.
  473. */
  474. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  475. "Driver successfully bound to device\n"));
  476. return 0;
  477. }
  478. static int acpi_start_single_object(struct acpi_device *device)
  479. {
  480. int result = 0;
  481. struct acpi_driver *driver;
  482. if (!(driver = device->driver))
  483. return 0;
  484. if (driver->ops.start) {
  485. result = driver->ops.start(device);
  486. if (result && driver->ops.remove)
  487. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  488. }
  489. return result;
  490. }
  491. /**
  492. * acpi_bus_register_driver - register a driver with the ACPI bus
  493. * @driver: driver being registered
  494. *
  495. * Registers a driver with the ACPI bus. Searches the namespace for all
  496. * devices that match the driver's criteria and binds. Returns zero for
  497. * success or a negative error status for failure.
  498. */
  499. int acpi_bus_register_driver(struct acpi_driver *driver)
  500. {
  501. int ret;
  502. if (acpi_disabled)
  503. return -ENODEV;
  504. driver->drv.name = driver->name;
  505. driver->drv.bus = &acpi_bus_type;
  506. driver->drv.owner = driver->owner;
  507. ret = driver_register(&driver->drv);
  508. return ret;
  509. }
  510. EXPORT_SYMBOL(acpi_bus_register_driver);
  511. /**
  512. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  513. * @driver: driver to unregister
  514. *
  515. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  516. * devices that match the driver's criteria and unbinds.
  517. */
  518. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  519. {
  520. driver_unregister(&driver->drv);
  521. }
  522. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  523. /* --------------------------------------------------------------------------
  524. Device Enumeration
  525. -------------------------------------------------------------------------- */
  526. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  527. {
  528. acpi_status status;
  529. int ret;
  530. struct acpi_device *device;
  531. /*
  532. * Fixed hardware devices do not appear in the namespace and do not
  533. * have handles, but we fabricate acpi_devices for them, so we have
  534. * to deal with them specially.
  535. */
  536. if (handle == NULL)
  537. return acpi_root;
  538. do {
  539. status = acpi_get_parent(handle, &handle);
  540. if (status == AE_NULL_ENTRY)
  541. return NULL;
  542. if (ACPI_FAILURE(status))
  543. return acpi_root;
  544. ret = acpi_bus_get_device(handle, &device);
  545. if (ret == 0)
  546. return device;
  547. } while (1);
  548. }
  549. acpi_status
  550. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  551. {
  552. acpi_status status;
  553. acpi_handle tmp;
  554. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  555. union acpi_object *obj;
  556. status = acpi_get_handle(handle, "_EJD", &tmp);
  557. if (ACPI_FAILURE(status))
  558. return status;
  559. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  560. if (ACPI_SUCCESS(status)) {
  561. obj = buffer.pointer;
  562. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  563. ejd);
  564. kfree(buffer.pointer);
  565. }
  566. return status;
  567. }
  568. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  569. void acpi_bus_data_handler(acpi_handle handle, void *context)
  570. {
  571. /* TBD */
  572. return;
  573. }
  574. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  575. {
  576. device->performance.state = ACPI_STATE_UNKNOWN;
  577. return 0;
  578. }
  579. static acpi_status
  580. acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
  581. union acpi_object *package)
  582. {
  583. int i = 0;
  584. union acpi_object *element = NULL;
  585. if (!device || !package || (package->package.count < 2))
  586. return AE_BAD_PARAMETER;
  587. element = &(package->package.elements[0]);
  588. if (!element)
  589. return AE_BAD_PARAMETER;
  590. if (element->type == ACPI_TYPE_PACKAGE) {
  591. if ((element->package.count < 2) ||
  592. (element->package.elements[0].type !=
  593. ACPI_TYPE_LOCAL_REFERENCE)
  594. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  595. return AE_BAD_DATA;
  596. device->wakeup.gpe_device =
  597. element->package.elements[0].reference.handle;
  598. device->wakeup.gpe_number =
  599. (u32) element->package.elements[1].integer.value;
  600. } else if (element->type == ACPI_TYPE_INTEGER) {
  601. device->wakeup.gpe_number = element->integer.value;
  602. } else
  603. return AE_BAD_DATA;
  604. element = &(package->package.elements[1]);
  605. if (element->type != ACPI_TYPE_INTEGER) {
  606. return AE_BAD_DATA;
  607. }
  608. device->wakeup.sleep_state = element->integer.value;
  609. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  610. return AE_NO_MEMORY;
  611. }
  612. device->wakeup.resources.count = package->package.count - 2;
  613. for (i = 0; i < device->wakeup.resources.count; i++) {
  614. element = &(package->package.elements[i + 2]);
  615. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  616. return AE_BAD_DATA;
  617. device->wakeup.resources.handles[i] = element->reference.handle;
  618. }
  619. return AE_OK;
  620. }
  621. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  622. {
  623. struct acpi_device_id button_device_ids[] = {
  624. {"PNP0C0D", 0},
  625. {"PNP0C0C", 0},
  626. {"PNP0C0E", 0},
  627. {"", 0},
  628. };
  629. acpi_status status;
  630. acpi_event_status event_status;
  631. device->wakeup.run_wake_count = 0;
  632. device->wakeup.flags.notifier_present = 0;
  633. /* Power button, Lid switch always enable wakeup */
  634. if (!acpi_match_device_ids(device, button_device_ids)) {
  635. device->wakeup.flags.run_wake = 1;
  636. device->wakeup.flags.always_enabled = 1;
  637. return;
  638. }
  639. status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
  640. ACPI_NOT_ISR, &event_status);
  641. if (status == AE_OK)
  642. device->wakeup.flags.run_wake =
  643. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  644. }
  645. static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  646. {
  647. acpi_status status = 0;
  648. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  649. union acpi_object *package = NULL;
  650. int psw_error;
  651. /* _PRW */
  652. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  653. if (ACPI_FAILURE(status)) {
  654. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  655. goto end;
  656. }
  657. package = (union acpi_object *)buffer.pointer;
  658. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  659. if (ACPI_FAILURE(status)) {
  660. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  661. goto end;
  662. }
  663. kfree(buffer.pointer);
  664. device->wakeup.flags.valid = 1;
  665. device->wakeup.prepare_count = 0;
  666. acpi_bus_set_run_wake_flags(device);
  667. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  668. * system for the ACPI device with the _PRW object.
  669. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  670. * So it is necessary to call _DSW object first. Only when it is not
  671. * present will the _PSW object used.
  672. */
  673. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  674. if (psw_error)
  675. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  676. "error in _DSW or _PSW evaluation\n"));
  677. end:
  678. if (ACPI_FAILURE(status))
  679. device->flags.wake_capable = 0;
  680. return 0;
  681. }
  682. static int acpi_bus_get_power_flags(struct acpi_device *device)
  683. {
  684. acpi_status status = 0;
  685. acpi_handle handle = NULL;
  686. u32 i = 0;
  687. /*
  688. * Power Management Flags
  689. */
  690. status = acpi_get_handle(device->handle, "_PSC", &handle);
  691. if (ACPI_SUCCESS(status))
  692. device->power.flags.explicit_get = 1;
  693. status = acpi_get_handle(device->handle, "_IRC", &handle);
  694. if (ACPI_SUCCESS(status))
  695. device->power.flags.inrush_current = 1;
  696. /*
  697. * Enumerate supported power management states
  698. */
  699. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  700. struct acpi_device_power_state *ps = &device->power.states[i];
  701. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  702. /* Evaluate "_PRx" to se if power resources are referenced */
  703. acpi_evaluate_reference(device->handle, object_name, NULL,
  704. &ps->resources);
  705. if (ps->resources.count) {
  706. device->power.flags.power_resources = 1;
  707. ps->flags.valid = 1;
  708. }
  709. /* Evaluate "_PSx" to see if we can do explicit sets */
  710. object_name[2] = 'S';
  711. status = acpi_get_handle(device->handle, object_name, &handle);
  712. if (ACPI_SUCCESS(status)) {
  713. ps->flags.explicit_set = 1;
  714. ps->flags.valid = 1;
  715. }
  716. /* State is valid if we have some power control */
  717. if (ps->resources.count || ps->flags.explicit_set)
  718. ps->flags.valid = 1;
  719. ps->power = -1; /* Unknown - driver assigned */
  720. ps->latency = -1; /* Unknown - driver assigned */
  721. }
  722. /* Set defaults for D0 and D3 states (always valid) */
  723. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  724. device->power.states[ACPI_STATE_D0].power = 100;
  725. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  726. device->power.states[ACPI_STATE_D3].power = 0;
  727. /* TBD: System wake support and resource requirements. */
  728. device->power.state = ACPI_STATE_UNKNOWN;
  729. acpi_bus_get_power(device->handle, &(device->power.state));
  730. return 0;
  731. }
  732. static int acpi_bus_get_flags(struct acpi_device *device)
  733. {
  734. acpi_status status = AE_OK;
  735. acpi_handle temp = NULL;
  736. /* Presence of _STA indicates 'dynamic_status' */
  737. status = acpi_get_handle(device->handle, "_STA", &temp);
  738. if (ACPI_SUCCESS(status))
  739. device->flags.dynamic_status = 1;
  740. /* Presence of _RMV indicates 'removable' */
  741. status = acpi_get_handle(device->handle, "_RMV", &temp);
  742. if (ACPI_SUCCESS(status))
  743. device->flags.removable = 1;
  744. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  745. status = acpi_get_handle(device->handle, "_EJD", &temp);
  746. if (ACPI_SUCCESS(status))
  747. device->flags.ejectable = 1;
  748. else {
  749. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  750. if (ACPI_SUCCESS(status))
  751. device->flags.ejectable = 1;
  752. }
  753. /* Presence of _LCK indicates 'lockable' */
  754. status = acpi_get_handle(device->handle, "_LCK", &temp);
  755. if (ACPI_SUCCESS(status))
  756. device->flags.lockable = 1;
  757. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  758. status = acpi_get_handle(device->handle, "_PS0", &temp);
  759. if (ACPI_FAILURE(status))
  760. status = acpi_get_handle(device->handle, "_PR0", &temp);
  761. if (ACPI_SUCCESS(status))
  762. device->flags.power_manageable = 1;
  763. /* Presence of _PRW indicates wake capable */
  764. status = acpi_get_handle(device->handle, "_PRW", &temp);
  765. if (ACPI_SUCCESS(status))
  766. device->flags.wake_capable = 1;
  767. /* TBD: Performance management */
  768. return 0;
  769. }
  770. static void acpi_device_get_busid(struct acpi_device *device)
  771. {
  772. char bus_id[5] = { '?', 0 };
  773. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  774. int i = 0;
  775. /*
  776. * Bus ID
  777. * ------
  778. * The device's Bus ID is simply the object name.
  779. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  780. */
  781. if (ACPI_IS_ROOT_DEVICE(device)) {
  782. strcpy(device->pnp.bus_id, "ACPI");
  783. return;
  784. }
  785. switch (device->device_type) {
  786. case ACPI_BUS_TYPE_POWER_BUTTON:
  787. strcpy(device->pnp.bus_id, "PWRF");
  788. break;
  789. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  790. strcpy(device->pnp.bus_id, "SLPF");
  791. break;
  792. default:
  793. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  794. /* Clean up trailing underscores (if any) */
  795. for (i = 3; i > 1; i--) {
  796. if (bus_id[i] == '_')
  797. bus_id[i] = '\0';
  798. else
  799. break;
  800. }
  801. strcpy(device->pnp.bus_id, bus_id);
  802. break;
  803. }
  804. }
  805. /*
  806. * acpi_bay_match - see if a device is an ejectable driver bay
  807. *
  808. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  809. * then we can safely call it an ejectable drive bay
  810. */
  811. static int acpi_bay_match(struct acpi_device *device){
  812. acpi_status status;
  813. acpi_handle handle;
  814. acpi_handle tmp;
  815. acpi_handle phandle;
  816. handle = device->handle;
  817. status = acpi_get_handle(handle, "_EJ0", &tmp);
  818. if (ACPI_FAILURE(status))
  819. return -ENODEV;
  820. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  821. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  822. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  823. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  824. return 0;
  825. if (acpi_get_parent(handle, &phandle))
  826. return -ENODEV;
  827. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  828. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  829. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  830. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  831. return 0;
  832. return -ENODEV;
  833. }
  834. /*
  835. * acpi_dock_match - see if a device has a _DCK method
  836. */
  837. static int acpi_dock_match(struct acpi_device *device)
  838. {
  839. acpi_handle tmp;
  840. return acpi_get_handle(device->handle, "_DCK", &tmp);
  841. }
  842. char *acpi_device_hid(struct acpi_device *device)
  843. {
  844. struct acpi_hardware_id *hid;
  845. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  846. return hid->id;
  847. }
  848. EXPORT_SYMBOL(acpi_device_hid);
  849. static void acpi_add_id(struct acpi_device *device, const char *dev_id)
  850. {
  851. struct acpi_hardware_id *id;
  852. id = kmalloc(sizeof(*id), GFP_KERNEL);
  853. if (!id)
  854. return;
  855. id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
  856. if (!id->id) {
  857. kfree(id);
  858. return;
  859. }
  860. strcpy(id->id, dev_id);
  861. list_add_tail(&id->list, &device->pnp.ids);
  862. }
  863. static void acpi_device_set_id(struct acpi_device *device)
  864. {
  865. acpi_status status;
  866. struct acpi_device_info *info;
  867. struct acpica_device_id_list *cid_list;
  868. int i;
  869. switch (device->device_type) {
  870. case ACPI_BUS_TYPE_DEVICE:
  871. if (ACPI_IS_ROOT_DEVICE(device)) {
  872. acpi_add_id(device, ACPI_SYSTEM_HID);
  873. break;
  874. } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
  875. /* \_SB_, the only root-level namespace device */
  876. acpi_add_id(device, ACPI_BUS_HID);
  877. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  878. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  879. break;
  880. }
  881. status = acpi_get_object_info(device->handle, &info);
  882. if (ACPI_FAILURE(status)) {
  883. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  884. return;
  885. }
  886. if (info->valid & ACPI_VALID_HID)
  887. acpi_add_id(device, info->hardware_id.string);
  888. if (info->valid & ACPI_VALID_CID) {
  889. cid_list = &info->compatible_id_list;
  890. for (i = 0; i < cid_list->count; i++)
  891. acpi_add_id(device, cid_list->ids[i].string);
  892. }
  893. if (info->valid & ACPI_VALID_ADR) {
  894. device->pnp.bus_address = info->address;
  895. device->flags.bus_address = 1;
  896. }
  897. kfree(info);
  898. /*
  899. * Some devices don't reliably have _HIDs & _CIDs, so add
  900. * synthetic HIDs to make sure drivers can find them.
  901. */
  902. if (acpi_is_video_device(device))
  903. acpi_add_id(device, ACPI_VIDEO_HID);
  904. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  905. acpi_add_id(device, ACPI_BAY_HID);
  906. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  907. acpi_add_id(device, ACPI_DOCK_HID);
  908. break;
  909. case ACPI_BUS_TYPE_POWER:
  910. acpi_add_id(device, ACPI_POWER_HID);
  911. break;
  912. case ACPI_BUS_TYPE_PROCESSOR:
  913. acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
  914. break;
  915. case ACPI_BUS_TYPE_THERMAL:
  916. acpi_add_id(device, ACPI_THERMAL_HID);
  917. break;
  918. case ACPI_BUS_TYPE_POWER_BUTTON:
  919. acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
  920. break;
  921. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  922. acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
  923. break;
  924. }
  925. /*
  926. * We build acpi_devices for some objects that don't have _HID or _CID,
  927. * e.g., PCI bridges and slots. Drivers can't bind to these objects,
  928. * but we do use them indirectly by traversing the acpi_device tree.
  929. * This generic ID isn't useful for driver binding, but it provides
  930. * the useful property that "every acpi_device has an ID."
  931. */
  932. if (list_empty(&device->pnp.ids))
  933. acpi_add_id(device, "device");
  934. }
  935. static int acpi_device_set_context(struct acpi_device *device)
  936. {
  937. acpi_status status;
  938. /*
  939. * Context
  940. * -------
  941. * Attach this 'struct acpi_device' to the ACPI object. This makes
  942. * resolutions from handle->device very efficient. Fixed hardware
  943. * devices have no handles, so we skip them.
  944. */
  945. if (!device->handle)
  946. return 0;
  947. status = acpi_attach_data(device->handle,
  948. acpi_bus_data_handler, device);
  949. if (ACPI_SUCCESS(status))
  950. return 0;
  951. printk(KERN_ERR PREFIX "Error attaching device data\n");
  952. return -ENODEV;
  953. }
  954. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  955. {
  956. if (!dev)
  957. return -EINVAL;
  958. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  959. device_release_driver(&dev->dev);
  960. if (!rmdevice)
  961. return 0;
  962. /*
  963. * unbind _ADR-Based Devices when hot removal
  964. */
  965. if (dev->flags.bus_address) {
  966. if ((dev->parent) && (dev->parent->ops.unbind))
  967. dev->parent->ops.unbind(dev);
  968. }
  969. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  970. return 0;
  971. }
  972. static int acpi_add_single_object(struct acpi_device **child,
  973. acpi_handle handle, int type,
  974. unsigned long long sta,
  975. struct acpi_bus_ops *ops)
  976. {
  977. int result;
  978. struct acpi_device *device;
  979. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  980. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  981. if (!device) {
  982. printk(KERN_ERR PREFIX "Memory allocation error\n");
  983. return -ENOMEM;
  984. }
  985. INIT_LIST_HEAD(&device->pnp.ids);
  986. device->device_type = type;
  987. device->handle = handle;
  988. device->parent = acpi_bus_get_parent(handle);
  989. device->bus_ops = *ops; /* workround for not call .start */
  990. STRUCT_TO_INT(device->status) = sta;
  991. acpi_device_get_busid(device);
  992. /*
  993. * Flags
  994. * -----
  995. * Note that we only look for object handles -- cannot evaluate objects
  996. * until we know the device is present and properly initialized.
  997. */
  998. result = acpi_bus_get_flags(device);
  999. if (result)
  1000. goto end;
  1001. /*
  1002. * Initialize Device
  1003. * -----------------
  1004. * TBD: Synch with Core's enumeration/initialization process.
  1005. */
  1006. acpi_device_set_id(device);
  1007. /*
  1008. * Power Management
  1009. * ----------------
  1010. */
  1011. if (device->flags.power_manageable) {
  1012. result = acpi_bus_get_power_flags(device);
  1013. if (result)
  1014. goto end;
  1015. }
  1016. /*
  1017. * Wakeup device management
  1018. *-----------------------
  1019. */
  1020. if (device->flags.wake_capable) {
  1021. result = acpi_bus_get_wakeup_device_flags(device);
  1022. if (result)
  1023. goto end;
  1024. }
  1025. /*
  1026. * Performance Management
  1027. * ----------------------
  1028. */
  1029. if (device->flags.performance_manageable) {
  1030. result = acpi_bus_get_perf_flags(device);
  1031. if (result)
  1032. goto end;
  1033. }
  1034. if ((result = acpi_device_set_context(device)))
  1035. goto end;
  1036. result = acpi_device_register(device);
  1037. /*
  1038. * Bind _ADR-Based Devices when hot add
  1039. */
  1040. if (device->flags.bus_address) {
  1041. if (device->parent && device->parent->ops.bind)
  1042. device->parent->ops.bind(device);
  1043. }
  1044. end:
  1045. if (!result) {
  1046. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1047. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1048. "Adding %s [%s] parent %s\n", dev_name(&device->dev),
  1049. (char *) buffer.pointer,
  1050. device->parent ? dev_name(&device->parent->dev) :
  1051. "(null)"));
  1052. kfree(buffer.pointer);
  1053. *child = device;
  1054. } else
  1055. acpi_device_release(&device->dev);
  1056. return result;
  1057. }
  1058. #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
  1059. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
  1060. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1061. unsigned long long *sta)
  1062. {
  1063. acpi_status status;
  1064. acpi_object_type acpi_type;
  1065. status = acpi_get_type(handle, &acpi_type);
  1066. if (ACPI_FAILURE(status))
  1067. return -ENODEV;
  1068. switch (acpi_type) {
  1069. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1070. case ACPI_TYPE_DEVICE:
  1071. *type = ACPI_BUS_TYPE_DEVICE;
  1072. status = acpi_bus_get_status_handle(handle, sta);
  1073. if (ACPI_FAILURE(status))
  1074. return -ENODEV;
  1075. break;
  1076. case ACPI_TYPE_PROCESSOR:
  1077. *type = ACPI_BUS_TYPE_PROCESSOR;
  1078. status = acpi_bus_get_status_handle(handle, sta);
  1079. if (ACPI_FAILURE(status))
  1080. return -ENODEV;
  1081. break;
  1082. case ACPI_TYPE_THERMAL:
  1083. *type = ACPI_BUS_TYPE_THERMAL;
  1084. *sta = ACPI_STA_DEFAULT;
  1085. break;
  1086. case ACPI_TYPE_POWER:
  1087. *type = ACPI_BUS_TYPE_POWER;
  1088. *sta = ACPI_STA_DEFAULT;
  1089. break;
  1090. default:
  1091. return -ENODEV;
  1092. }
  1093. return 0;
  1094. }
  1095. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
  1096. void *context, void **return_value)
  1097. {
  1098. struct acpi_bus_ops *ops = context;
  1099. int type;
  1100. unsigned long long sta;
  1101. struct acpi_device *device;
  1102. acpi_status status;
  1103. int result;
  1104. result = acpi_bus_type_and_status(handle, &type, &sta);
  1105. if (result)
  1106. return AE_OK;
  1107. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1108. !(sta & ACPI_STA_DEVICE_FUNCTIONING))
  1109. return AE_CTRL_DEPTH;
  1110. /*
  1111. * We may already have an acpi_device from a previous enumeration. If
  1112. * so, we needn't add it again, but we may still have to start it.
  1113. */
  1114. device = NULL;
  1115. acpi_bus_get_device(handle, &device);
  1116. if (ops->acpi_op_add && !device)
  1117. acpi_add_single_object(&device, handle, type, sta, ops);
  1118. if (!device)
  1119. return AE_CTRL_DEPTH;
  1120. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1121. status = acpi_start_single_object(device);
  1122. if (ACPI_FAILURE(status))
  1123. return AE_CTRL_DEPTH;
  1124. }
  1125. if (!*return_value)
  1126. *return_value = device;
  1127. return AE_OK;
  1128. }
  1129. static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
  1130. struct acpi_device **child)
  1131. {
  1132. acpi_status status;
  1133. void *device = NULL;
  1134. status = acpi_bus_check_add(handle, 0, ops, &device);
  1135. if (ACPI_SUCCESS(status))
  1136. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1137. acpi_bus_check_add, NULL, ops, &device);
  1138. if (child)
  1139. *child = device;
  1140. if (device)
  1141. return 0;
  1142. else
  1143. return -ENODEV;
  1144. }
  1145. /*
  1146. * acpi_bus_add and acpi_bus_start
  1147. *
  1148. * scan a given ACPI tree and (probably recently hot-plugged)
  1149. * create and add or starts found devices.
  1150. *
  1151. * If no devices were found -ENODEV is returned which does not
  1152. * mean that this is a real error, there just have been no suitable
  1153. * ACPI objects in the table trunk from which the kernel could create
  1154. * a device and add/start an appropriate driver.
  1155. */
  1156. int
  1157. acpi_bus_add(struct acpi_device **child,
  1158. struct acpi_device *parent, acpi_handle handle, int type)
  1159. {
  1160. struct acpi_bus_ops ops;
  1161. memset(&ops, 0, sizeof(ops));
  1162. ops.acpi_op_add = 1;
  1163. return acpi_bus_scan(handle, &ops, child);
  1164. }
  1165. EXPORT_SYMBOL(acpi_bus_add);
  1166. int acpi_bus_start(struct acpi_device *device)
  1167. {
  1168. struct acpi_bus_ops ops;
  1169. if (!device)
  1170. return -EINVAL;
  1171. memset(&ops, 0, sizeof(ops));
  1172. ops.acpi_op_start = 1;
  1173. return acpi_bus_scan(device->handle, &ops, NULL);
  1174. }
  1175. EXPORT_SYMBOL(acpi_bus_start);
  1176. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1177. {
  1178. acpi_status status;
  1179. struct acpi_device *parent, *child;
  1180. acpi_handle phandle, chandle;
  1181. acpi_object_type type;
  1182. u32 level = 1;
  1183. int err = 0;
  1184. parent = start;
  1185. phandle = start->handle;
  1186. child = chandle = NULL;
  1187. while ((level > 0) && parent && (!err)) {
  1188. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1189. chandle, &chandle);
  1190. /*
  1191. * If this scope is exhausted then move our way back up.
  1192. */
  1193. if (ACPI_FAILURE(status)) {
  1194. level--;
  1195. chandle = phandle;
  1196. acpi_get_parent(phandle, &phandle);
  1197. child = parent;
  1198. parent = parent->parent;
  1199. if (level == 0)
  1200. err = acpi_bus_remove(child, rmdevice);
  1201. else
  1202. err = acpi_bus_remove(child, 1);
  1203. continue;
  1204. }
  1205. status = acpi_get_type(chandle, &type);
  1206. if (ACPI_FAILURE(status)) {
  1207. continue;
  1208. }
  1209. /*
  1210. * If there is a device corresponding to chandle then
  1211. * parse it (depth-first).
  1212. */
  1213. if (acpi_bus_get_device(chandle, &child) == 0) {
  1214. level++;
  1215. phandle = chandle;
  1216. chandle = NULL;
  1217. parent = child;
  1218. }
  1219. continue;
  1220. }
  1221. return err;
  1222. }
  1223. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1224. static int acpi_bus_scan_fixed(void)
  1225. {
  1226. int result = 0;
  1227. struct acpi_device *device = NULL;
  1228. struct acpi_bus_ops ops;
  1229. memset(&ops, 0, sizeof(ops));
  1230. ops.acpi_op_add = 1;
  1231. ops.acpi_op_start = 1;
  1232. /*
  1233. * Enumerate all fixed-feature devices.
  1234. */
  1235. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1236. result = acpi_add_single_object(&device, NULL,
  1237. ACPI_BUS_TYPE_POWER_BUTTON,
  1238. ACPI_STA_DEFAULT,
  1239. &ops);
  1240. }
  1241. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1242. result = acpi_add_single_object(&device, NULL,
  1243. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1244. ACPI_STA_DEFAULT,
  1245. &ops);
  1246. }
  1247. return result;
  1248. }
  1249. int __init acpi_scan_init(void)
  1250. {
  1251. int result;
  1252. struct acpi_bus_ops ops;
  1253. memset(&ops, 0, sizeof(ops));
  1254. ops.acpi_op_add = 1;
  1255. ops.acpi_op_start = 1;
  1256. result = bus_register(&acpi_bus_type);
  1257. if (result) {
  1258. /* We don't want to quit even if we failed to add suspend/resume */
  1259. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1260. }
  1261. /*
  1262. * Enumerate devices in the ACPI namespace.
  1263. */
  1264. result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
  1265. if (!result)
  1266. result = acpi_bus_scan_fixed();
  1267. if (result)
  1268. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1269. return result;
  1270. }