scan.c 39 KB

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