scan.c 38 KB

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