scan.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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 int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  622. {
  623. acpi_status status = 0;
  624. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  625. union acpi_object *package = NULL;
  626. int psw_error;
  627. struct acpi_device_id button_device_ids[] = {
  628. {"PNP0C0D", 0},
  629. {"PNP0C0C", 0},
  630. {"PNP0C0E", 0},
  631. {"", 0},
  632. };
  633. /* _PRW */
  634. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  635. if (ACPI_FAILURE(status)) {
  636. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  637. goto end;
  638. }
  639. package = (union acpi_object *)buffer.pointer;
  640. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  641. if (ACPI_FAILURE(status)) {
  642. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  643. goto end;
  644. }
  645. kfree(buffer.pointer);
  646. device->wakeup.flags.valid = 1;
  647. device->wakeup.prepare_count = 0;
  648. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  649. * system for the ACPI device with the _PRW object.
  650. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  651. * So it is necessary to call _DSW object first. Only when it is not
  652. * present will the _PSW object used.
  653. */
  654. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  655. if (psw_error)
  656. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  657. "error in _DSW or _PSW evaluation\n"));
  658. /* Power button, Lid switch always enable wakeup */
  659. if (!acpi_match_device_ids(device, button_device_ids))
  660. device->wakeup.flags.run_wake = 1;
  661. end:
  662. if (ACPI_FAILURE(status))
  663. device->flags.wake_capable = 0;
  664. return 0;
  665. }
  666. static int acpi_bus_get_power_flags(struct acpi_device *device)
  667. {
  668. acpi_status status = 0;
  669. acpi_handle handle = NULL;
  670. u32 i = 0;
  671. /*
  672. * Power Management Flags
  673. */
  674. status = acpi_get_handle(device->handle, "_PSC", &handle);
  675. if (ACPI_SUCCESS(status))
  676. device->power.flags.explicit_get = 1;
  677. status = acpi_get_handle(device->handle, "_IRC", &handle);
  678. if (ACPI_SUCCESS(status))
  679. device->power.flags.inrush_current = 1;
  680. /*
  681. * Enumerate supported power management states
  682. */
  683. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  684. struct acpi_device_power_state *ps = &device->power.states[i];
  685. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  686. /* Evaluate "_PRx" to se if power resources are referenced */
  687. acpi_evaluate_reference(device->handle, object_name, NULL,
  688. &ps->resources);
  689. if (ps->resources.count) {
  690. device->power.flags.power_resources = 1;
  691. ps->flags.valid = 1;
  692. }
  693. /* Evaluate "_PSx" to see if we can do explicit sets */
  694. object_name[2] = 'S';
  695. status = acpi_get_handle(device->handle, object_name, &handle);
  696. if (ACPI_SUCCESS(status)) {
  697. ps->flags.explicit_set = 1;
  698. ps->flags.valid = 1;
  699. }
  700. /* State is valid if we have some power control */
  701. if (ps->resources.count || ps->flags.explicit_set)
  702. ps->flags.valid = 1;
  703. ps->power = -1; /* Unknown - driver assigned */
  704. ps->latency = -1; /* Unknown - driver assigned */
  705. }
  706. /* Set defaults for D0 and D3 states (always valid) */
  707. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  708. device->power.states[ACPI_STATE_D0].power = 100;
  709. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  710. device->power.states[ACPI_STATE_D3].power = 0;
  711. /* TBD: System wake support and resource requirements. */
  712. device->power.state = ACPI_STATE_UNKNOWN;
  713. acpi_bus_get_power(device->handle, &(device->power.state));
  714. return 0;
  715. }
  716. static int acpi_bus_get_flags(struct acpi_device *device)
  717. {
  718. acpi_status status = AE_OK;
  719. acpi_handle temp = NULL;
  720. /* Presence of _STA indicates 'dynamic_status' */
  721. status = acpi_get_handle(device->handle, "_STA", &temp);
  722. if (ACPI_SUCCESS(status))
  723. device->flags.dynamic_status = 1;
  724. /* Presence of _RMV indicates 'removable' */
  725. status = acpi_get_handle(device->handle, "_RMV", &temp);
  726. if (ACPI_SUCCESS(status))
  727. device->flags.removable = 1;
  728. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  729. status = acpi_get_handle(device->handle, "_EJD", &temp);
  730. if (ACPI_SUCCESS(status))
  731. device->flags.ejectable = 1;
  732. else {
  733. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  734. if (ACPI_SUCCESS(status))
  735. device->flags.ejectable = 1;
  736. }
  737. /* Presence of _LCK indicates 'lockable' */
  738. status = acpi_get_handle(device->handle, "_LCK", &temp);
  739. if (ACPI_SUCCESS(status))
  740. device->flags.lockable = 1;
  741. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  742. status = acpi_get_handle(device->handle, "_PS0", &temp);
  743. if (ACPI_FAILURE(status))
  744. status = acpi_get_handle(device->handle, "_PR0", &temp);
  745. if (ACPI_SUCCESS(status))
  746. device->flags.power_manageable = 1;
  747. /* Presence of _PRW indicates wake capable */
  748. status = acpi_get_handle(device->handle, "_PRW", &temp);
  749. if (ACPI_SUCCESS(status))
  750. device->flags.wake_capable = 1;
  751. /* TBD: Performance management */
  752. return 0;
  753. }
  754. static void acpi_device_get_busid(struct acpi_device *device)
  755. {
  756. char bus_id[5] = { '?', 0 };
  757. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  758. int i = 0;
  759. /*
  760. * Bus ID
  761. * ------
  762. * The device's Bus ID is simply the object name.
  763. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  764. */
  765. if (ACPI_IS_ROOT_DEVICE(device)) {
  766. strcpy(device->pnp.bus_id, "ACPI");
  767. return;
  768. }
  769. switch (device->device_type) {
  770. case ACPI_BUS_TYPE_POWER_BUTTON:
  771. strcpy(device->pnp.bus_id, "PWRF");
  772. break;
  773. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  774. strcpy(device->pnp.bus_id, "SLPF");
  775. break;
  776. default:
  777. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  778. /* Clean up trailing underscores (if any) */
  779. for (i = 3; i > 1; i--) {
  780. if (bus_id[i] == '_')
  781. bus_id[i] = '\0';
  782. else
  783. break;
  784. }
  785. strcpy(device->pnp.bus_id, bus_id);
  786. break;
  787. }
  788. }
  789. /*
  790. * acpi_bay_match - see if a device is an ejectable driver bay
  791. *
  792. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  793. * then we can safely call it an ejectable drive bay
  794. */
  795. static int acpi_bay_match(struct acpi_device *device){
  796. acpi_status status;
  797. acpi_handle handle;
  798. acpi_handle tmp;
  799. acpi_handle phandle;
  800. handle = device->handle;
  801. status = acpi_get_handle(handle, "_EJ0", &tmp);
  802. if (ACPI_FAILURE(status))
  803. return -ENODEV;
  804. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  805. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  806. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  807. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  808. return 0;
  809. if (acpi_get_parent(handle, &phandle))
  810. return -ENODEV;
  811. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  812. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  813. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  814. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  815. return 0;
  816. return -ENODEV;
  817. }
  818. /*
  819. * acpi_dock_match - see if a device has a _DCK method
  820. */
  821. static int acpi_dock_match(struct acpi_device *device)
  822. {
  823. acpi_handle tmp;
  824. return acpi_get_handle(device->handle, "_DCK", &tmp);
  825. }
  826. char *acpi_device_hid(struct acpi_device *device)
  827. {
  828. struct acpi_hardware_id *hid;
  829. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  830. return hid->id;
  831. }
  832. EXPORT_SYMBOL(acpi_device_hid);
  833. static void acpi_add_id(struct acpi_device *device, const char *dev_id)
  834. {
  835. struct acpi_hardware_id *id;
  836. id = kmalloc(sizeof(*id), GFP_KERNEL);
  837. if (!id)
  838. return;
  839. id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
  840. if (!id->id) {
  841. kfree(id);
  842. return;
  843. }
  844. strcpy(id->id, dev_id);
  845. list_add_tail(&id->list, &device->pnp.ids);
  846. }
  847. static void acpi_device_set_id(struct acpi_device *device)
  848. {
  849. acpi_status status;
  850. struct acpi_device_info *info;
  851. struct acpica_device_id_list *cid_list;
  852. int i;
  853. switch (device->device_type) {
  854. case ACPI_BUS_TYPE_DEVICE:
  855. if (ACPI_IS_ROOT_DEVICE(device)) {
  856. acpi_add_id(device, ACPI_SYSTEM_HID);
  857. break;
  858. } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
  859. /* \_SB_, the only root-level namespace device */
  860. acpi_add_id(device, ACPI_BUS_HID);
  861. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  862. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  863. break;
  864. }
  865. status = acpi_get_object_info(device->handle, &info);
  866. if (ACPI_FAILURE(status)) {
  867. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  868. return;
  869. }
  870. if (info->valid & ACPI_VALID_HID)
  871. acpi_add_id(device, info->hardware_id.string);
  872. if (info->valid & ACPI_VALID_CID) {
  873. cid_list = &info->compatible_id_list;
  874. for (i = 0; i < cid_list->count; i++)
  875. acpi_add_id(device, cid_list->ids[i].string);
  876. }
  877. if (info->valid & ACPI_VALID_ADR) {
  878. device->pnp.bus_address = info->address;
  879. device->flags.bus_address = 1;
  880. }
  881. kfree(info);
  882. /*
  883. * Some devices don't reliably have _HIDs & _CIDs, so add
  884. * synthetic HIDs to make sure drivers can find them.
  885. */
  886. if (acpi_is_video_device(device))
  887. acpi_add_id(device, ACPI_VIDEO_HID);
  888. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  889. acpi_add_id(device, ACPI_BAY_HID);
  890. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  891. acpi_add_id(device, ACPI_DOCK_HID);
  892. break;
  893. case ACPI_BUS_TYPE_POWER:
  894. acpi_add_id(device, ACPI_POWER_HID);
  895. break;
  896. case ACPI_BUS_TYPE_PROCESSOR:
  897. acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
  898. break;
  899. case ACPI_BUS_TYPE_THERMAL:
  900. acpi_add_id(device, ACPI_THERMAL_HID);
  901. break;
  902. case ACPI_BUS_TYPE_POWER_BUTTON:
  903. acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
  904. break;
  905. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  906. acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
  907. break;
  908. }
  909. /*
  910. * We build acpi_devices for some objects that don't have _HID or _CID,
  911. * e.g., PCI bridges and slots. Drivers can't bind to these objects,
  912. * but we do use them indirectly by traversing the acpi_device tree.
  913. * This generic ID isn't useful for driver binding, but it provides
  914. * the useful property that "every acpi_device has an ID."
  915. */
  916. if (list_empty(&device->pnp.ids))
  917. acpi_add_id(device, "device");
  918. }
  919. static int acpi_device_set_context(struct acpi_device *device)
  920. {
  921. acpi_status status;
  922. /*
  923. * Context
  924. * -------
  925. * Attach this 'struct acpi_device' to the ACPI object. This makes
  926. * resolutions from handle->device very efficient. Fixed hardware
  927. * devices have no handles, so we skip them.
  928. */
  929. if (!device->handle)
  930. return 0;
  931. status = acpi_attach_data(device->handle,
  932. acpi_bus_data_handler, device);
  933. if (ACPI_SUCCESS(status))
  934. return 0;
  935. printk(KERN_ERR PREFIX "Error attaching device data\n");
  936. return -ENODEV;
  937. }
  938. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  939. {
  940. if (!dev)
  941. return -EINVAL;
  942. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  943. device_release_driver(&dev->dev);
  944. if (!rmdevice)
  945. return 0;
  946. /*
  947. * unbind _ADR-Based Devices when hot removal
  948. */
  949. if (dev->flags.bus_address) {
  950. if ((dev->parent) && (dev->parent->ops.unbind))
  951. dev->parent->ops.unbind(dev);
  952. }
  953. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  954. return 0;
  955. }
  956. static int acpi_add_single_object(struct acpi_device **child,
  957. acpi_handle handle, int type,
  958. unsigned long long sta,
  959. struct acpi_bus_ops *ops)
  960. {
  961. int result;
  962. struct acpi_device *device;
  963. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  964. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  965. if (!device) {
  966. printk(KERN_ERR PREFIX "Memory allocation error\n");
  967. return -ENOMEM;
  968. }
  969. INIT_LIST_HEAD(&device->pnp.ids);
  970. device->device_type = type;
  971. device->handle = handle;
  972. device->parent = acpi_bus_get_parent(handle);
  973. device->bus_ops = *ops; /* workround for not call .start */
  974. STRUCT_TO_INT(device->status) = sta;
  975. acpi_device_get_busid(device);
  976. /*
  977. * Flags
  978. * -----
  979. * Note that we only look for object handles -- cannot evaluate objects
  980. * until we know the device is present and properly initialized.
  981. */
  982. result = acpi_bus_get_flags(device);
  983. if (result)
  984. goto end;
  985. /*
  986. * Initialize Device
  987. * -----------------
  988. * TBD: Synch with Core's enumeration/initialization process.
  989. */
  990. acpi_device_set_id(device);
  991. /*
  992. * Power Management
  993. * ----------------
  994. */
  995. if (device->flags.power_manageable) {
  996. result = acpi_bus_get_power_flags(device);
  997. if (result)
  998. goto end;
  999. }
  1000. /*
  1001. * Wakeup device management
  1002. *-----------------------
  1003. */
  1004. if (device->flags.wake_capable) {
  1005. result = acpi_bus_get_wakeup_device_flags(device);
  1006. if (result)
  1007. goto end;
  1008. }
  1009. /*
  1010. * Performance Management
  1011. * ----------------------
  1012. */
  1013. if (device->flags.performance_manageable) {
  1014. result = acpi_bus_get_perf_flags(device);
  1015. if (result)
  1016. goto end;
  1017. }
  1018. if ((result = acpi_device_set_context(device)))
  1019. goto end;
  1020. result = acpi_device_register(device);
  1021. /*
  1022. * Bind _ADR-Based Devices when hot add
  1023. */
  1024. if (device->flags.bus_address) {
  1025. if (device->parent && device->parent->ops.bind)
  1026. device->parent->ops.bind(device);
  1027. }
  1028. end:
  1029. if (!result) {
  1030. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1031. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1032. "Adding %s [%s] parent %s\n", dev_name(&device->dev),
  1033. (char *) buffer.pointer,
  1034. device->parent ? dev_name(&device->parent->dev) :
  1035. "(null)"));
  1036. kfree(buffer.pointer);
  1037. *child = device;
  1038. } else
  1039. acpi_device_release(&device->dev);
  1040. return result;
  1041. }
  1042. #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
  1043. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
  1044. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1045. unsigned long long *sta)
  1046. {
  1047. acpi_status status;
  1048. acpi_object_type acpi_type;
  1049. status = acpi_get_type(handle, &acpi_type);
  1050. if (ACPI_FAILURE(status))
  1051. return -ENODEV;
  1052. switch (acpi_type) {
  1053. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1054. case ACPI_TYPE_DEVICE:
  1055. *type = ACPI_BUS_TYPE_DEVICE;
  1056. status = acpi_bus_get_status_handle(handle, sta);
  1057. if (ACPI_FAILURE(status))
  1058. return -ENODEV;
  1059. break;
  1060. case ACPI_TYPE_PROCESSOR:
  1061. *type = ACPI_BUS_TYPE_PROCESSOR;
  1062. status = acpi_bus_get_status_handle(handle, sta);
  1063. if (ACPI_FAILURE(status))
  1064. return -ENODEV;
  1065. break;
  1066. case ACPI_TYPE_THERMAL:
  1067. *type = ACPI_BUS_TYPE_THERMAL;
  1068. *sta = ACPI_STA_DEFAULT;
  1069. break;
  1070. case ACPI_TYPE_POWER:
  1071. *type = ACPI_BUS_TYPE_POWER;
  1072. *sta = ACPI_STA_DEFAULT;
  1073. break;
  1074. default:
  1075. return -ENODEV;
  1076. }
  1077. return 0;
  1078. }
  1079. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
  1080. void *context, void **return_value)
  1081. {
  1082. struct acpi_bus_ops *ops = context;
  1083. int type;
  1084. unsigned long long sta;
  1085. struct acpi_device *device;
  1086. acpi_status status;
  1087. int result;
  1088. result = acpi_bus_type_and_status(handle, &type, &sta);
  1089. if (result)
  1090. return AE_OK;
  1091. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1092. !(sta & ACPI_STA_DEVICE_FUNCTIONING))
  1093. return AE_CTRL_DEPTH;
  1094. /*
  1095. * We may already have an acpi_device from a previous enumeration. If
  1096. * so, we needn't add it again, but we may still have to start it.
  1097. */
  1098. device = NULL;
  1099. acpi_bus_get_device(handle, &device);
  1100. if (ops->acpi_op_add && !device)
  1101. acpi_add_single_object(&device, handle, type, sta, ops);
  1102. if (!device)
  1103. return AE_CTRL_DEPTH;
  1104. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1105. status = acpi_start_single_object(device);
  1106. if (ACPI_FAILURE(status))
  1107. return AE_CTRL_DEPTH;
  1108. }
  1109. if (!*return_value)
  1110. *return_value = device;
  1111. return AE_OK;
  1112. }
  1113. static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
  1114. struct acpi_device **child)
  1115. {
  1116. acpi_status status;
  1117. void *device = NULL;
  1118. status = acpi_bus_check_add(handle, 0, ops, &device);
  1119. if (ACPI_SUCCESS(status))
  1120. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1121. acpi_bus_check_add, NULL, ops, &device);
  1122. if (child)
  1123. *child = device;
  1124. if (device)
  1125. return 0;
  1126. else
  1127. return -ENODEV;
  1128. }
  1129. /*
  1130. * acpi_bus_add and acpi_bus_start
  1131. *
  1132. * scan a given ACPI tree and (probably recently hot-plugged)
  1133. * create and add or starts found devices.
  1134. *
  1135. * If no devices were found -ENODEV is returned which does not
  1136. * mean that this is a real error, there just have been no suitable
  1137. * ACPI objects in the table trunk from which the kernel could create
  1138. * a device and add/start an appropriate driver.
  1139. */
  1140. int
  1141. acpi_bus_add(struct acpi_device **child,
  1142. struct acpi_device *parent, acpi_handle handle, int type)
  1143. {
  1144. struct acpi_bus_ops ops;
  1145. memset(&ops, 0, sizeof(ops));
  1146. ops.acpi_op_add = 1;
  1147. return acpi_bus_scan(handle, &ops, child);
  1148. }
  1149. EXPORT_SYMBOL(acpi_bus_add);
  1150. int acpi_bus_start(struct acpi_device *device)
  1151. {
  1152. struct acpi_bus_ops ops;
  1153. if (!device)
  1154. return -EINVAL;
  1155. memset(&ops, 0, sizeof(ops));
  1156. ops.acpi_op_start = 1;
  1157. return acpi_bus_scan(device->handle, &ops, NULL);
  1158. }
  1159. EXPORT_SYMBOL(acpi_bus_start);
  1160. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1161. {
  1162. acpi_status status;
  1163. struct acpi_device *parent, *child;
  1164. acpi_handle phandle, chandle;
  1165. acpi_object_type type;
  1166. u32 level = 1;
  1167. int err = 0;
  1168. parent = start;
  1169. phandle = start->handle;
  1170. child = chandle = NULL;
  1171. while ((level > 0) && parent && (!err)) {
  1172. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1173. chandle, &chandle);
  1174. /*
  1175. * If this scope is exhausted then move our way back up.
  1176. */
  1177. if (ACPI_FAILURE(status)) {
  1178. level--;
  1179. chandle = phandle;
  1180. acpi_get_parent(phandle, &phandle);
  1181. child = parent;
  1182. parent = parent->parent;
  1183. if (level == 0)
  1184. err = acpi_bus_remove(child, rmdevice);
  1185. else
  1186. err = acpi_bus_remove(child, 1);
  1187. continue;
  1188. }
  1189. status = acpi_get_type(chandle, &type);
  1190. if (ACPI_FAILURE(status)) {
  1191. continue;
  1192. }
  1193. /*
  1194. * If there is a device corresponding to chandle then
  1195. * parse it (depth-first).
  1196. */
  1197. if (acpi_bus_get_device(chandle, &child) == 0) {
  1198. level++;
  1199. phandle = chandle;
  1200. chandle = NULL;
  1201. parent = child;
  1202. }
  1203. continue;
  1204. }
  1205. return err;
  1206. }
  1207. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1208. static int acpi_bus_scan_fixed(void)
  1209. {
  1210. int result = 0;
  1211. struct acpi_device *device = NULL;
  1212. struct acpi_bus_ops ops;
  1213. memset(&ops, 0, sizeof(ops));
  1214. ops.acpi_op_add = 1;
  1215. ops.acpi_op_start = 1;
  1216. /*
  1217. * Enumerate all fixed-feature devices.
  1218. */
  1219. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1220. result = acpi_add_single_object(&device, NULL,
  1221. ACPI_BUS_TYPE_POWER_BUTTON,
  1222. ACPI_STA_DEFAULT,
  1223. &ops);
  1224. }
  1225. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1226. result = acpi_add_single_object(&device, NULL,
  1227. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1228. ACPI_STA_DEFAULT,
  1229. &ops);
  1230. }
  1231. return result;
  1232. }
  1233. int __init acpi_scan_init(void)
  1234. {
  1235. int result;
  1236. struct acpi_bus_ops ops;
  1237. memset(&ops, 0, sizeof(ops));
  1238. ops.acpi_op_add = 1;
  1239. ops.acpi_op_start = 1;
  1240. result = bus_register(&acpi_bus_type);
  1241. if (result) {
  1242. /* We don't want to quit even if we failed to add suspend/resume */
  1243. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1244. }
  1245. /*
  1246. * Enumerate devices in the ACPI namespace.
  1247. */
  1248. result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
  1249. if (!result)
  1250. result = acpi_bus_scan_fixed();
  1251. if (result)
  1252. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1253. return result;
  1254. }