scan.c 41 KB

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