scan.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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 <linux/nls.h>
  13. #include <linux/dma-mapping.h>
  14. #include <asm/pgtable.h>
  15. #include "internal.h"
  16. #define _COMPONENT ACPI_BUS_COMPONENT
  17. ACPI_MODULE_NAME("scan");
  18. extern struct acpi_device *acpi_root;
  19. #define ACPI_BUS_CLASS "system_bus"
  20. #define ACPI_BUS_HID "LNXSYBUS"
  21. #define ACPI_BUS_DEVICE_NAME "System Bus"
  22. #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
  23. #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
  24. /*
  25. * If set, devices will be hot-removed even if they cannot be put offline
  26. * gracefully (from the kernel's standpoint).
  27. */
  28. bool acpi_force_hot_remove;
  29. static const char *dummy_hid = "device";
  30. static LIST_HEAD(acpi_dep_list);
  31. static DEFINE_MUTEX(acpi_dep_list_lock);
  32. LIST_HEAD(acpi_bus_id_list);
  33. static DEFINE_MUTEX(acpi_scan_lock);
  34. static LIST_HEAD(acpi_scan_handlers_list);
  35. DEFINE_MUTEX(acpi_device_lock);
  36. LIST_HEAD(acpi_wakeup_device_list);
  37. static DEFINE_MUTEX(acpi_hp_context_lock);
  38. /*
  39. * The UART device described by the SPCR table is the only object which needs
  40. * special-casing. Everything else is covered by ACPI namespace paths in STAO
  41. * table.
  42. */
  43. static u64 spcr_uart_addr;
  44. struct acpi_dep_data {
  45. struct list_head node;
  46. acpi_handle master;
  47. acpi_handle slave;
  48. };
  49. void acpi_scan_lock_acquire(void)
  50. {
  51. mutex_lock(&acpi_scan_lock);
  52. }
  53. EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
  54. void acpi_scan_lock_release(void)
  55. {
  56. mutex_unlock(&acpi_scan_lock);
  57. }
  58. EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
  59. void acpi_lock_hp_context(void)
  60. {
  61. mutex_lock(&acpi_hp_context_lock);
  62. }
  63. void acpi_unlock_hp_context(void)
  64. {
  65. mutex_unlock(&acpi_hp_context_lock);
  66. }
  67. void acpi_initialize_hp_context(struct acpi_device *adev,
  68. struct acpi_hotplug_context *hp,
  69. int (*notify)(struct acpi_device *, u32),
  70. void (*uevent)(struct acpi_device *, u32))
  71. {
  72. acpi_lock_hp_context();
  73. hp->notify = notify;
  74. hp->uevent = uevent;
  75. acpi_set_hp_context(adev, hp);
  76. acpi_unlock_hp_context();
  77. }
  78. EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
  79. int acpi_scan_add_handler(struct acpi_scan_handler *handler)
  80. {
  81. if (!handler)
  82. return -EINVAL;
  83. list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
  84. return 0;
  85. }
  86. int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
  87. const char *hotplug_profile_name)
  88. {
  89. int error;
  90. error = acpi_scan_add_handler(handler);
  91. if (error)
  92. return error;
  93. acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
  94. return 0;
  95. }
  96. bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
  97. {
  98. struct acpi_device_physical_node *pn;
  99. bool offline = true;
  100. /*
  101. * acpi_container_offline() calls this for all of the container's
  102. * children under the container's physical_node_lock lock.
  103. */
  104. mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
  105. list_for_each_entry(pn, &adev->physical_node_list, node)
  106. if (device_supports_offline(pn->dev) && !pn->dev->offline) {
  107. if (uevent)
  108. kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
  109. offline = false;
  110. break;
  111. }
  112. mutex_unlock(&adev->physical_node_lock);
  113. return offline;
  114. }
  115. static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
  116. void **ret_p)
  117. {
  118. struct acpi_device *device = NULL;
  119. struct acpi_device_physical_node *pn;
  120. bool second_pass = (bool)data;
  121. acpi_status status = AE_OK;
  122. if (acpi_bus_get_device(handle, &device))
  123. return AE_OK;
  124. if (device->handler && !device->handler->hotplug.enabled) {
  125. *ret_p = &device->dev;
  126. return AE_SUPPORT;
  127. }
  128. mutex_lock(&device->physical_node_lock);
  129. list_for_each_entry(pn, &device->physical_node_list, node) {
  130. int ret;
  131. if (second_pass) {
  132. /* Skip devices offlined by the first pass. */
  133. if (pn->put_online)
  134. continue;
  135. } else {
  136. pn->put_online = false;
  137. }
  138. ret = device_offline(pn->dev);
  139. if (acpi_force_hot_remove)
  140. continue;
  141. if (ret >= 0) {
  142. pn->put_online = !ret;
  143. } else {
  144. *ret_p = pn->dev;
  145. if (second_pass) {
  146. status = AE_ERROR;
  147. break;
  148. }
  149. }
  150. }
  151. mutex_unlock(&device->physical_node_lock);
  152. return status;
  153. }
  154. static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
  155. void **ret_p)
  156. {
  157. struct acpi_device *device = NULL;
  158. struct acpi_device_physical_node *pn;
  159. if (acpi_bus_get_device(handle, &device))
  160. return AE_OK;
  161. mutex_lock(&device->physical_node_lock);
  162. list_for_each_entry(pn, &device->physical_node_list, node)
  163. if (pn->put_online) {
  164. device_online(pn->dev);
  165. pn->put_online = false;
  166. }
  167. mutex_unlock(&device->physical_node_lock);
  168. return AE_OK;
  169. }
  170. static int acpi_scan_try_to_offline(struct acpi_device *device)
  171. {
  172. acpi_handle handle = device->handle;
  173. struct device *errdev = NULL;
  174. acpi_status status;
  175. /*
  176. * Carry out two passes here and ignore errors in the first pass,
  177. * because if the devices in question are memory blocks and
  178. * CONFIG_MEMCG is set, one of the blocks may hold data structures
  179. * that the other blocks depend on, but it is not known in advance which
  180. * block holds them.
  181. *
  182. * If the first pass is successful, the second one isn't needed, though.
  183. */
  184. status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  185. NULL, acpi_bus_offline, (void *)false,
  186. (void **)&errdev);
  187. if (status == AE_SUPPORT) {
  188. dev_warn(errdev, "Offline disabled.\n");
  189. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  190. acpi_bus_online, NULL, NULL, NULL);
  191. return -EPERM;
  192. }
  193. acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
  194. if (errdev) {
  195. errdev = NULL;
  196. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  197. NULL, acpi_bus_offline, (void *)true,
  198. (void **)&errdev);
  199. if (!errdev || acpi_force_hot_remove)
  200. acpi_bus_offline(handle, 0, (void *)true,
  201. (void **)&errdev);
  202. if (errdev && !acpi_force_hot_remove) {
  203. dev_warn(errdev, "Offline failed.\n");
  204. acpi_bus_online(handle, 0, NULL, NULL);
  205. acpi_walk_namespace(ACPI_TYPE_ANY, handle,
  206. ACPI_UINT32_MAX, acpi_bus_online,
  207. NULL, NULL, NULL);
  208. return -EBUSY;
  209. }
  210. }
  211. return 0;
  212. }
  213. static int acpi_scan_hot_remove(struct acpi_device *device)
  214. {
  215. acpi_handle handle = device->handle;
  216. unsigned long long sta;
  217. acpi_status status;
  218. if (device->handler && device->handler->hotplug.demand_offline
  219. && !acpi_force_hot_remove) {
  220. if (!acpi_scan_is_offline(device, true))
  221. return -EBUSY;
  222. } else {
  223. int error = acpi_scan_try_to_offline(device);
  224. if (error)
  225. return error;
  226. }
  227. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  228. "Hot-removing device %s...\n", dev_name(&device->dev)));
  229. acpi_bus_trim(device);
  230. acpi_evaluate_lck(handle, 0);
  231. /*
  232. * TBD: _EJD support.
  233. */
  234. status = acpi_evaluate_ej0(handle);
  235. if (status == AE_NOT_FOUND)
  236. return -ENODEV;
  237. else if (ACPI_FAILURE(status))
  238. return -EIO;
  239. /*
  240. * Verify if eject was indeed successful. If not, log an error
  241. * message. No need to call _OST since _EJ0 call was made OK.
  242. */
  243. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  244. if (ACPI_FAILURE(status)) {
  245. acpi_handle_warn(handle,
  246. "Status check after eject failed (0x%x)\n", status);
  247. } else if (sta & ACPI_STA_DEVICE_ENABLED) {
  248. acpi_handle_warn(handle,
  249. "Eject incomplete - status 0x%llx\n", sta);
  250. }
  251. return 0;
  252. }
  253. static int acpi_scan_device_not_present(struct acpi_device *adev)
  254. {
  255. if (!acpi_device_enumerated(adev)) {
  256. dev_warn(&adev->dev, "Still not present\n");
  257. return -EALREADY;
  258. }
  259. acpi_bus_trim(adev);
  260. return 0;
  261. }
  262. static int acpi_scan_device_check(struct acpi_device *adev)
  263. {
  264. int error;
  265. acpi_bus_get_status(adev);
  266. if (adev->status.present || adev->status.functional) {
  267. /*
  268. * This function is only called for device objects for which
  269. * matching scan handlers exist. The only situation in which
  270. * the scan handler is not attached to this device object yet
  271. * is when the device has just appeared (either it wasn't
  272. * present at all before or it was removed and then added
  273. * again).
  274. */
  275. if (adev->handler) {
  276. dev_warn(&adev->dev, "Already enumerated\n");
  277. return -EALREADY;
  278. }
  279. error = acpi_bus_scan(adev->handle);
  280. if (error) {
  281. dev_warn(&adev->dev, "Namespace scan failure\n");
  282. return error;
  283. }
  284. if (!adev->handler) {
  285. dev_warn(&adev->dev, "Enumeration failure\n");
  286. error = -ENODEV;
  287. }
  288. } else {
  289. error = acpi_scan_device_not_present(adev);
  290. }
  291. return error;
  292. }
  293. static int acpi_scan_bus_check(struct acpi_device *adev)
  294. {
  295. struct acpi_scan_handler *handler = adev->handler;
  296. struct acpi_device *child;
  297. int error;
  298. acpi_bus_get_status(adev);
  299. if (!(adev->status.present || adev->status.functional)) {
  300. acpi_scan_device_not_present(adev);
  301. return 0;
  302. }
  303. if (handler && handler->hotplug.scan_dependent)
  304. return handler->hotplug.scan_dependent(adev);
  305. error = acpi_bus_scan(adev->handle);
  306. if (error) {
  307. dev_warn(&adev->dev, "Namespace scan failure\n");
  308. return error;
  309. }
  310. list_for_each_entry(child, &adev->children, node) {
  311. error = acpi_scan_bus_check(child);
  312. if (error)
  313. return error;
  314. }
  315. return 0;
  316. }
  317. static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
  318. {
  319. switch (type) {
  320. case ACPI_NOTIFY_BUS_CHECK:
  321. return acpi_scan_bus_check(adev);
  322. case ACPI_NOTIFY_DEVICE_CHECK:
  323. return acpi_scan_device_check(adev);
  324. case ACPI_NOTIFY_EJECT_REQUEST:
  325. case ACPI_OST_EC_OSPM_EJECT:
  326. if (adev->handler && !adev->handler->hotplug.enabled) {
  327. dev_info(&adev->dev, "Eject disabled\n");
  328. return -EPERM;
  329. }
  330. acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
  331. ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
  332. return acpi_scan_hot_remove(adev);
  333. }
  334. return -EINVAL;
  335. }
  336. void acpi_device_hotplug(struct acpi_device *adev, u32 src)
  337. {
  338. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  339. int error = -ENODEV;
  340. lock_device_hotplug();
  341. mutex_lock(&acpi_scan_lock);
  342. /*
  343. * The device object's ACPI handle cannot become invalid as long as we
  344. * are holding acpi_scan_lock, but it might have become invalid before
  345. * that lock was acquired.
  346. */
  347. if (adev->handle == INVALID_ACPI_HANDLE)
  348. goto err_out;
  349. if (adev->flags.is_dock_station) {
  350. error = dock_notify(adev, src);
  351. } else if (adev->flags.hotplug_notify) {
  352. error = acpi_generic_hotplug_event(adev, src);
  353. if (error == -EPERM) {
  354. ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
  355. goto err_out;
  356. }
  357. } else {
  358. int (*notify)(struct acpi_device *, u32);
  359. acpi_lock_hp_context();
  360. notify = adev->hp ? adev->hp->notify : NULL;
  361. acpi_unlock_hp_context();
  362. /*
  363. * There may be additional notify handlers for device objects
  364. * without the .event() callback, so ignore them here.
  365. */
  366. if (notify)
  367. error = notify(adev, src);
  368. else
  369. goto out;
  370. }
  371. if (!error)
  372. ost_code = ACPI_OST_SC_SUCCESS;
  373. err_out:
  374. acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
  375. out:
  376. acpi_bus_put_acpi_device(adev);
  377. mutex_unlock(&acpi_scan_lock);
  378. unlock_device_hotplug();
  379. }
  380. static void acpi_free_power_resources_lists(struct acpi_device *device)
  381. {
  382. int i;
  383. if (device->wakeup.flags.valid)
  384. acpi_power_resources_list_free(&device->wakeup.resources);
  385. if (!device->power.flags.power_resources)
  386. return;
  387. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  388. struct acpi_device_power_state *ps = &device->power.states[i];
  389. acpi_power_resources_list_free(&ps->resources);
  390. }
  391. }
  392. static void acpi_device_release(struct device *dev)
  393. {
  394. struct acpi_device *acpi_dev = to_acpi_device(dev);
  395. acpi_free_properties(acpi_dev);
  396. acpi_free_pnp_ids(&acpi_dev->pnp);
  397. acpi_free_power_resources_lists(acpi_dev);
  398. kfree(acpi_dev);
  399. }
  400. static void acpi_device_del(struct acpi_device *device)
  401. {
  402. struct acpi_device_bus_id *acpi_device_bus_id;
  403. mutex_lock(&acpi_device_lock);
  404. if (device->parent)
  405. list_del(&device->node);
  406. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
  407. if (!strcmp(acpi_device_bus_id->bus_id,
  408. acpi_device_hid(device))) {
  409. if (acpi_device_bus_id->instance_no > 0)
  410. acpi_device_bus_id->instance_no--;
  411. else {
  412. list_del(&acpi_device_bus_id->node);
  413. kfree(acpi_device_bus_id);
  414. }
  415. break;
  416. }
  417. list_del(&device->wakeup_list);
  418. mutex_unlock(&acpi_device_lock);
  419. acpi_power_add_remove_device(device, false);
  420. acpi_device_remove_files(device);
  421. if (device->remove)
  422. device->remove(device);
  423. device_del(&device->dev);
  424. }
  425. static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
  426. static LIST_HEAD(acpi_device_del_list);
  427. static DEFINE_MUTEX(acpi_device_del_lock);
  428. static void acpi_device_del_work_fn(struct work_struct *work_not_used)
  429. {
  430. for (;;) {
  431. struct acpi_device *adev;
  432. mutex_lock(&acpi_device_del_lock);
  433. if (list_empty(&acpi_device_del_list)) {
  434. mutex_unlock(&acpi_device_del_lock);
  435. break;
  436. }
  437. adev = list_first_entry(&acpi_device_del_list,
  438. struct acpi_device, del_list);
  439. list_del(&adev->del_list);
  440. mutex_unlock(&acpi_device_del_lock);
  441. blocking_notifier_call_chain(&acpi_reconfig_chain,
  442. ACPI_RECONFIG_DEVICE_REMOVE, adev);
  443. acpi_device_del(adev);
  444. /*
  445. * Drop references to all power resources that might have been
  446. * used by the device.
  447. */
  448. acpi_power_transition(adev, ACPI_STATE_D3_COLD);
  449. put_device(&adev->dev);
  450. }
  451. }
  452. /**
  453. * acpi_scan_drop_device - Drop an ACPI device object.
  454. * @handle: Handle of an ACPI namespace node, not used.
  455. * @context: Address of the ACPI device object to drop.
  456. *
  457. * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
  458. * namespace node the device object pointed to by @context is attached to.
  459. *
  460. * The unregistration is carried out asynchronously to avoid running
  461. * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
  462. * ensure the correct ordering (the device objects must be unregistered in the
  463. * same order in which the corresponding namespace nodes are deleted).
  464. */
  465. static void acpi_scan_drop_device(acpi_handle handle, void *context)
  466. {
  467. static DECLARE_WORK(work, acpi_device_del_work_fn);
  468. struct acpi_device *adev = context;
  469. mutex_lock(&acpi_device_del_lock);
  470. /*
  471. * Use the ACPI hotplug workqueue which is ordered, so this work item
  472. * won't run after any hotplug work items submitted subsequently. That
  473. * prevents attempts to register device objects identical to those being
  474. * deleted from happening concurrently (such attempts result from
  475. * hotplug events handled via the ACPI hotplug workqueue). It also will
  476. * run after all of the work items submitted previosuly, which helps
  477. * those work items to ensure that they are not accessing stale device
  478. * objects.
  479. */
  480. if (list_empty(&acpi_device_del_list))
  481. acpi_queue_hotplug_work(&work);
  482. list_add_tail(&adev->del_list, &acpi_device_del_list);
  483. /* Make acpi_ns_validate_handle() return NULL for this handle. */
  484. adev->handle = INVALID_ACPI_HANDLE;
  485. mutex_unlock(&acpi_device_del_lock);
  486. }
  487. static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
  488. void (*callback)(void *))
  489. {
  490. acpi_status status;
  491. if (!device)
  492. return -EINVAL;
  493. status = acpi_get_data_full(handle, acpi_scan_drop_device,
  494. (void **)device, callback);
  495. if (ACPI_FAILURE(status) || !*device) {
  496. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  497. handle));
  498. return -ENODEV;
  499. }
  500. return 0;
  501. }
  502. int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
  503. {
  504. return acpi_get_device_data(handle, device, NULL);
  505. }
  506. EXPORT_SYMBOL(acpi_bus_get_device);
  507. static void get_acpi_device(void *dev)
  508. {
  509. if (dev)
  510. get_device(&((struct acpi_device *)dev)->dev);
  511. }
  512. struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
  513. {
  514. struct acpi_device *adev = NULL;
  515. acpi_get_device_data(handle, &adev, get_acpi_device);
  516. return adev;
  517. }
  518. void acpi_bus_put_acpi_device(struct acpi_device *adev)
  519. {
  520. put_device(&adev->dev);
  521. }
  522. int acpi_device_add(struct acpi_device *device,
  523. void (*release)(struct device *))
  524. {
  525. int result;
  526. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  527. int found = 0;
  528. if (device->handle) {
  529. acpi_status status;
  530. status = acpi_attach_data(device->handle, acpi_scan_drop_device,
  531. device);
  532. if (ACPI_FAILURE(status)) {
  533. acpi_handle_err(device->handle,
  534. "Unable to attach device data\n");
  535. return -ENODEV;
  536. }
  537. }
  538. /*
  539. * Linkage
  540. * -------
  541. * Link this device to its parent and siblings.
  542. */
  543. INIT_LIST_HEAD(&device->children);
  544. INIT_LIST_HEAD(&device->node);
  545. INIT_LIST_HEAD(&device->wakeup_list);
  546. INIT_LIST_HEAD(&device->physical_node_list);
  547. INIT_LIST_HEAD(&device->del_list);
  548. mutex_init(&device->physical_node_lock);
  549. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  550. if (!new_bus_id) {
  551. pr_err(PREFIX "Memory allocation error\n");
  552. result = -ENOMEM;
  553. goto err_detach;
  554. }
  555. mutex_lock(&acpi_device_lock);
  556. /*
  557. * Find suitable bus_id and instance number in acpi_bus_id_list
  558. * If failed, create one and link it into acpi_bus_id_list
  559. */
  560. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  561. if (!strcmp(acpi_device_bus_id->bus_id,
  562. acpi_device_hid(device))) {
  563. acpi_device_bus_id->instance_no++;
  564. found = 1;
  565. kfree(new_bus_id);
  566. break;
  567. }
  568. }
  569. if (!found) {
  570. acpi_device_bus_id = new_bus_id;
  571. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  572. acpi_device_bus_id->instance_no = 0;
  573. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  574. }
  575. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  576. if (device->parent)
  577. list_add_tail(&device->node, &device->parent->children);
  578. if (device->wakeup.flags.valid)
  579. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  580. mutex_unlock(&acpi_device_lock);
  581. if (device->parent)
  582. device->dev.parent = &device->parent->dev;
  583. device->dev.bus = &acpi_bus_type;
  584. device->dev.release = release;
  585. result = device_add(&device->dev);
  586. if (result) {
  587. dev_err(&device->dev, "Error registering device\n");
  588. goto err;
  589. }
  590. result = acpi_device_setup_files(device);
  591. if (result)
  592. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  593. dev_name(&device->dev));
  594. return 0;
  595. err:
  596. mutex_lock(&acpi_device_lock);
  597. if (device->parent)
  598. list_del(&device->node);
  599. list_del(&device->wakeup_list);
  600. mutex_unlock(&acpi_device_lock);
  601. err_detach:
  602. acpi_detach_data(device->handle, acpi_scan_drop_device);
  603. return result;
  604. }
  605. /* --------------------------------------------------------------------------
  606. Device Enumeration
  607. -------------------------------------------------------------------------- */
  608. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  609. {
  610. struct acpi_device *device = NULL;
  611. acpi_status status;
  612. /*
  613. * Fixed hardware devices do not appear in the namespace and do not
  614. * have handles, but we fabricate acpi_devices for them, so we have
  615. * to deal with them specially.
  616. */
  617. if (!handle)
  618. return acpi_root;
  619. do {
  620. status = acpi_get_parent(handle, &handle);
  621. if (ACPI_FAILURE(status))
  622. return status == AE_NULL_ENTRY ? NULL : acpi_root;
  623. } while (acpi_bus_get_device(handle, &device));
  624. return device;
  625. }
  626. acpi_status
  627. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  628. {
  629. acpi_status status;
  630. acpi_handle tmp;
  631. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  632. union acpi_object *obj;
  633. status = acpi_get_handle(handle, "_EJD", &tmp);
  634. if (ACPI_FAILURE(status))
  635. return status;
  636. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  637. if (ACPI_SUCCESS(status)) {
  638. obj = buffer.pointer;
  639. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  640. ejd);
  641. kfree(buffer.pointer);
  642. }
  643. return status;
  644. }
  645. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  646. static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  647. struct acpi_device_wakeup *wakeup)
  648. {
  649. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  650. union acpi_object *package = NULL;
  651. union acpi_object *element = NULL;
  652. acpi_status status;
  653. int err = -ENODATA;
  654. if (!wakeup)
  655. return -EINVAL;
  656. INIT_LIST_HEAD(&wakeup->resources);
  657. /* _PRW */
  658. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  659. if (ACPI_FAILURE(status)) {
  660. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  661. return err;
  662. }
  663. package = (union acpi_object *)buffer.pointer;
  664. if (!package || package->package.count < 2)
  665. goto out;
  666. element = &(package->package.elements[0]);
  667. if (!element)
  668. goto out;
  669. if (element->type == ACPI_TYPE_PACKAGE) {
  670. if ((element->package.count < 2) ||
  671. (element->package.elements[0].type !=
  672. ACPI_TYPE_LOCAL_REFERENCE)
  673. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  674. goto out;
  675. wakeup->gpe_device =
  676. element->package.elements[0].reference.handle;
  677. wakeup->gpe_number =
  678. (u32) element->package.elements[1].integer.value;
  679. } else if (element->type == ACPI_TYPE_INTEGER) {
  680. wakeup->gpe_device = NULL;
  681. wakeup->gpe_number = element->integer.value;
  682. } else {
  683. goto out;
  684. }
  685. element = &(package->package.elements[1]);
  686. if (element->type != ACPI_TYPE_INTEGER)
  687. goto out;
  688. wakeup->sleep_state = element->integer.value;
  689. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  690. if (err)
  691. goto out;
  692. if (!list_empty(&wakeup->resources)) {
  693. int sleep_state;
  694. err = acpi_power_wakeup_list_init(&wakeup->resources,
  695. &sleep_state);
  696. if (err) {
  697. acpi_handle_warn(handle, "Retrieving current states "
  698. "of wakeup power resources failed\n");
  699. acpi_power_resources_list_free(&wakeup->resources);
  700. goto out;
  701. }
  702. if (sleep_state < wakeup->sleep_state) {
  703. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  704. "(S%d) by S%d from power resources\n",
  705. (int)wakeup->sleep_state, sleep_state);
  706. wakeup->sleep_state = sleep_state;
  707. }
  708. }
  709. out:
  710. kfree(buffer.pointer);
  711. return err;
  712. }
  713. static void acpi_wakeup_gpe_init(struct acpi_device *device)
  714. {
  715. static const struct acpi_device_id button_device_ids[] = {
  716. {"PNP0C0C", 0},
  717. {"PNP0C0D", 0},
  718. {"PNP0C0E", 0},
  719. {"", 0},
  720. };
  721. struct acpi_device_wakeup *wakeup = &device->wakeup;
  722. acpi_status status;
  723. acpi_event_status event_status;
  724. wakeup->flags.notifier_present = 0;
  725. /* Power button, Lid switch always enable wakeup */
  726. if (!acpi_match_device_ids(device, button_device_ids)) {
  727. wakeup->flags.run_wake = 1;
  728. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  729. /* Do not use Lid/sleep button for S5 wakeup */
  730. if (wakeup->sleep_state == ACPI_STATE_S5)
  731. wakeup->sleep_state = ACPI_STATE_S4;
  732. }
  733. acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
  734. device_set_wakeup_capable(&device->dev, true);
  735. return;
  736. }
  737. acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
  738. wakeup->gpe_number);
  739. status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
  740. &event_status);
  741. if (ACPI_FAILURE(status))
  742. return;
  743. wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
  744. }
  745. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  746. {
  747. int err;
  748. /* Presence of _PRW indicates wake capable */
  749. if (!acpi_has_method(device->handle, "_PRW"))
  750. return;
  751. err = acpi_bus_extract_wakeup_device_power_package(device->handle,
  752. &device->wakeup);
  753. if (err) {
  754. dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
  755. return;
  756. }
  757. device->wakeup.flags.valid = 1;
  758. device->wakeup.prepare_count = 0;
  759. acpi_wakeup_gpe_init(device);
  760. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  761. * system for the ACPI device with the _PRW object.
  762. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  763. * So it is necessary to call _DSW object first. Only when it is not
  764. * present will the _PSW object used.
  765. */
  766. err = acpi_device_sleep_wake(device, 0, 0, 0);
  767. if (err)
  768. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  769. "error in _DSW or _PSW evaluation\n"));
  770. }
  771. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  772. {
  773. struct acpi_device_power_state *ps = &device->power.states[state];
  774. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  775. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  776. acpi_status status;
  777. INIT_LIST_HEAD(&ps->resources);
  778. /* Evaluate "_PRx" to get referenced power resources */
  779. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  780. if (ACPI_SUCCESS(status)) {
  781. union acpi_object *package = buffer.pointer;
  782. if (buffer.length && package
  783. && package->type == ACPI_TYPE_PACKAGE
  784. && package->package.count) {
  785. int err = acpi_extract_power_resources(package, 0,
  786. &ps->resources);
  787. if (!err)
  788. device->power.flags.power_resources = 1;
  789. }
  790. ACPI_FREE(buffer.pointer);
  791. }
  792. /* Evaluate "_PSx" to see if we can do explicit sets */
  793. pathname[2] = 'S';
  794. if (acpi_has_method(device->handle, pathname))
  795. ps->flags.explicit_set = 1;
  796. /* State is valid if there are means to put the device into it. */
  797. if (!list_empty(&ps->resources) || ps->flags.explicit_set)
  798. ps->flags.valid = 1;
  799. ps->power = -1; /* Unknown - driver assigned */
  800. ps->latency = -1; /* Unknown - driver assigned */
  801. }
  802. static void acpi_bus_get_power_flags(struct acpi_device *device)
  803. {
  804. u32 i;
  805. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  806. if (!acpi_has_method(device->handle, "_PS0") &&
  807. !acpi_has_method(device->handle, "_PR0"))
  808. return;
  809. device->flags.power_manageable = 1;
  810. /*
  811. * Power Management Flags
  812. */
  813. if (acpi_has_method(device->handle, "_PSC"))
  814. device->power.flags.explicit_get = 1;
  815. if (acpi_has_method(device->handle, "_IRC"))
  816. device->power.flags.inrush_current = 1;
  817. if (acpi_has_method(device->handle, "_DSW"))
  818. device->power.flags.dsw_present = 1;
  819. /*
  820. * Enumerate supported power management states
  821. */
  822. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  823. acpi_bus_init_power_state(device, i);
  824. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  825. if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
  826. device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
  827. /* Set defaults for D0 and D3hot states (always valid) */
  828. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  829. device->power.states[ACPI_STATE_D0].power = 100;
  830. device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
  831. if (acpi_bus_init_power(device))
  832. device->flags.power_manageable = 0;
  833. }
  834. static void acpi_bus_get_flags(struct acpi_device *device)
  835. {
  836. /* Presence of _STA indicates 'dynamic_status' */
  837. if (acpi_has_method(device->handle, "_STA"))
  838. device->flags.dynamic_status = 1;
  839. /* Presence of _RMV indicates 'removable' */
  840. if (acpi_has_method(device->handle, "_RMV"))
  841. device->flags.removable = 1;
  842. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  843. if (acpi_has_method(device->handle, "_EJD") ||
  844. acpi_has_method(device->handle, "_EJ0"))
  845. device->flags.ejectable = 1;
  846. }
  847. static void acpi_device_get_busid(struct acpi_device *device)
  848. {
  849. char bus_id[5] = { '?', 0 };
  850. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  851. int i = 0;
  852. /*
  853. * Bus ID
  854. * ------
  855. * The device's Bus ID is simply the object name.
  856. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  857. */
  858. if (ACPI_IS_ROOT_DEVICE(device)) {
  859. strcpy(device->pnp.bus_id, "ACPI");
  860. return;
  861. }
  862. switch (device->device_type) {
  863. case ACPI_BUS_TYPE_POWER_BUTTON:
  864. strcpy(device->pnp.bus_id, "PWRF");
  865. break;
  866. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  867. strcpy(device->pnp.bus_id, "SLPF");
  868. break;
  869. default:
  870. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  871. /* Clean up trailing underscores (if any) */
  872. for (i = 3; i > 1; i--) {
  873. if (bus_id[i] == '_')
  874. bus_id[i] = '\0';
  875. else
  876. break;
  877. }
  878. strcpy(device->pnp.bus_id, bus_id);
  879. break;
  880. }
  881. }
  882. /*
  883. * acpi_ata_match - see if an acpi object is an ATA device
  884. *
  885. * If an acpi object has one of the ACPI ATA methods defined,
  886. * then we can safely call it an ATA device.
  887. */
  888. bool acpi_ata_match(acpi_handle handle)
  889. {
  890. return acpi_has_method(handle, "_GTF") ||
  891. acpi_has_method(handle, "_GTM") ||
  892. acpi_has_method(handle, "_STM") ||
  893. acpi_has_method(handle, "_SDD");
  894. }
  895. /*
  896. * acpi_bay_match - see if an acpi object is an ejectable driver bay
  897. *
  898. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  899. * then we can safely call it an ejectable drive bay
  900. */
  901. bool acpi_bay_match(acpi_handle handle)
  902. {
  903. acpi_handle phandle;
  904. if (!acpi_has_method(handle, "_EJ0"))
  905. return false;
  906. if (acpi_ata_match(handle))
  907. return true;
  908. if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
  909. return false;
  910. return acpi_ata_match(phandle);
  911. }
  912. bool acpi_device_is_battery(struct acpi_device *adev)
  913. {
  914. struct acpi_hardware_id *hwid;
  915. list_for_each_entry(hwid, &adev->pnp.ids, list)
  916. if (!strcmp("PNP0C0A", hwid->id))
  917. return true;
  918. return false;
  919. }
  920. static bool is_ejectable_bay(struct acpi_device *adev)
  921. {
  922. acpi_handle handle = adev->handle;
  923. if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
  924. return true;
  925. return acpi_bay_match(handle);
  926. }
  927. /*
  928. * acpi_dock_match - see if an acpi object has a _DCK method
  929. */
  930. bool acpi_dock_match(acpi_handle handle)
  931. {
  932. return acpi_has_method(handle, "_DCK");
  933. }
  934. static acpi_status
  935. acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
  936. void **return_value)
  937. {
  938. long *cap = context;
  939. if (acpi_has_method(handle, "_BCM") &&
  940. acpi_has_method(handle, "_BCL")) {
  941. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
  942. "support\n"));
  943. *cap |= ACPI_VIDEO_BACKLIGHT;
  944. /* We have backlight support, no need to scan further */
  945. return AE_CTRL_TERMINATE;
  946. }
  947. return 0;
  948. }
  949. /* Returns true if the ACPI object is a video device which can be
  950. * handled by video.ko.
  951. * The device will get a Linux specific CID added in scan.c to
  952. * identify the device as an ACPI graphics device
  953. * Be aware that the graphics device may not be physically present
  954. * Use acpi_video_get_capabilities() to detect general ACPI video
  955. * capabilities of present cards
  956. */
  957. long acpi_is_video_device(acpi_handle handle)
  958. {
  959. long video_caps = 0;
  960. /* Is this device able to support video switching ? */
  961. if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
  962. video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
  963. /* Is this device able to retrieve a video ROM ? */
  964. if (acpi_has_method(handle, "_ROM"))
  965. video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
  966. /* Is this device able to configure which video head to be POSTed ? */
  967. if (acpi_has_method(handle, "_VPO") &&
  968. acpi_has_method(handle, "_GPD") &&
  969. acpi_has_method(handle, "_SPD"))
  970. video_caps |= ACPI_VIDEO_DEVICE_POSTING;
  971. /* Only check for backlight functionality if one of the above hit. */
  972. if (video_caps)
  973. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  974. ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
  975. &video_caps, NULL);
  976. return video_caps;
  977. }
  978. EXPORT_SYMBOL(acpi_is_video_device);
  979. const char *acpi_device_hid(struct acpi_device *device)
  980. {
  981. struct acpi_hardware_id *hid;
  982. if (list_empty(&device->pnp.ids))
  983. return dummy_hid;
  984. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  985. return hid->id;
  986. }
  987. EXPORT_SYMBOL(acpi_device_hid);
  988. static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
  989. {
  990. struct acpi_hardware_id *id;
  991. id = kmalloc(sizeof(*id), GFP_KERNEL);
  992. if (!id)
  993. return;
  994. id->id = kstrdup_const(dev_id, GFP_KERNEL);
  995. if (!id->id) {
  996. kfree(id);
  997. return;
  998. }
  999. list_add_tail(&id->list, &pnp->ids);
  1000. pnp->type.hardware_id = 1;
  1001. }
  1002. /*
  1003. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1004. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1005. * prefix. Work around this.
  1006. */
  1007. static bool acpi_ibm_smbus_match(acpi_handle handle)
  1008. {
  1009. char node_name[ACPI_PATH_SEGMENT_LENGTH];
  1010. struct acpi_buffer path = { sizeof(node_name), node_name };
  1011. if (!dmi_name_in_vendors("IBM"))
  1012. return false;
  1013. /* Look for SMBS object */
  1014. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
  1015. strcmp("SMBS", path.pointer))
  1016. return false;
  1017. /* Does it have the necessary (but misnamed) methods? */
  1018. if (acpi_has_method(handle, "SBI") &&
  1019. acpi_has_method(handle, "SBR") &&
  1020. acpi_has_method(handle, "SBW"))
  1021. return true;
  1022. return false;
  1023. }
  1024. static bool acpi_object_is_system_bus(acpi_handle handle)
  1025. {
  1026. acpi_handle tmp;
  1027. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
  1028. tmp == handle)
  1029. return true;
  1030. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
  1031. tmp == handle)
  1032. return true;
  1033. return false;
  1034. }
  1035. static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
  1036. int device_type)
  1037. {
  1038. acpi_status status;
  1039. struct acpi_device_info *info;
  1040. struct acpi_pnp_device_id_list *cid_list;
  1041. int i;
  1042. switch (device_type) {
  1043. case ACPI_BUS_TYPE_DEVICE:
  1044. if (handle == ACPI_ROOT_OBJECT) {
  1045. acpi_add_id(pnp, ACPI_SYSTEM_HID);
  1046. break;
  1047. }
  1048. status = acpi_get_object_info(handle, &info);
  1049. if (ACPI_FAILURE(status)) {
  1050. pr_err(PREFIX "%s: Error reading device info\n",
  1051. __func__);
  1052. return;
  1053. }
  1054. if (info->valid & ACPI_VALID_HID) {
  1055. acpi_add_id(pnp, info->hardware_id.string);
  1056. pnp->type.platform_id = 1;
  1057. }
  1058. if (info->valid & ACPI_VALID_CID) {
  1059. cid_list = &info->compatible_id_list;
  1060. for (i = 0; i < cid_list->count; i++)
  1061. acpi_add_id(pnp, cid_list->ids[i].string);
  1062. }
  1063. if (info->valid & ACPI_VALID_ADR) {
  1064. pnp->bus_address = info->address;
  1065. pnp->type.bus_address = 1;
  1066. }
  1067. if (info->valid & ACPI_VALID_UID)
  1068. pnp->unique_id = kstrdup(info->unique_id.string,
  1069. GFP_KERNEL);
  1070. if (info->valid & ACPI_VALID_CLS)
  1071. acpi_add_id(pnp, info->class_code.string);
  1072. kfree(info);
  1073. /*
  1074. * Some devices don't reliably have _HIDs & _CIDs, so add
  1075. * synthetic HIDs to make sure drivers can find them.
  1076. */
  1077. if (acpi_is_video_device(handle))
  1078. acpi_add_id(pnp, ACPI_VIDEO_HID);
  1079. else if (acpi_bay_match(handle))
  1080. acpi_add_id(pnp, ACPI_BAY_HID);
  1081. else if (acpi_dock_match(handle))
  1082. acpi_add_id(pnp, ACPI_DOCK_HID);
  1083. else if (acpi_ibm_smbus_match(handle))
  1084. acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
  1085. else if (list_empty(&pnp->ids) &&
  1086. acpi_object_is_system_bus(handle)) {
  1087. /* \_SB, \_TZ, LNXSYBUS */
  1088. acpi_add_id(pnp, ACPI_BUS_HID);
  1089. strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
  1090. strcpy(pnp->device_class, ACPI_BUS_CLASS);
  1091. }
  1092. break;
  1093. case ACPI_BUS_TYPE_POWER:
  1094. acpi_add_id(pnp, ACPI_POWER_HID);
  1095. break;
  1096. case ACPI_BUS_TYPE_PROCESSOR:
  1097. acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
  1098. break;
  1099. case ACPI_BUS_TYPE_THERMAL:
  1100. acpi_add_id(pnp, ACPI_THERMAL_HID);
  1101. break;
  1102. case ACPI_BUS_TYPE_POWER_BUTTON:
  1103. acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
  1104. break;
  1105. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1106. acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
  1107. break;
  1108. }
  1109. }
  1110. void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
  1111. {
  1112. struct acpi_hardware_id *id, *tmp;
  1113. list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
  1114. kfree_const(id->id);
  1115. kfree(id);
  1116. }
  1117. kfree(pnp->unique_id);
  1118. }
  1119. /**
  1120. * acpi_dma_supported - Check DMA support for the specified device.
  1121. * @adev: The pointer to acpi device
  1122. *
  1123. * Return false if DMA is not supported. Otherwise, return true
  1124. */
  1125. bool acpi_dma_supported(struct acpi_device *adev)
  1126. {
  1127. if (!adev)
  1128. return false;
  1129. if (adev->flags.cca_seen)
  1130. return true;
  1131. /*
  1132. * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
  1133. * DMA on "Intel platforms". Presumably that includes all x86 and
  1134. * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
  1135. */
  1136. if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1137. return true;
  1138. return false;
  1139. }
  1140. /**
  1141. * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
  1142. * @adev: The pointer to acpi device
  1143. *
  1144. * Return enum dev_dma_attr.
  1145. */
  1146. enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
  1147. {
  1148. if (!acpi_dma_supported(adev))
  1149. return DEV_DMA_NOT_SUPPORTED;
  1150. if (adev->flags.coherent_dma)
  1151. return DEV_DMA_COHERENT;
  1152. else
  1153. return DEV_DMA_NON_COHERENT;
  1154. }
  1155. static void acpi_init_coherency(struct acpi_device *adev)
  1156. {
  1157. unsigned long long cca = 0;
  1158. acpi_status status;
  1159. struct acpi_device *parent = adev->parent;
  1160. if (parent && parent->flags.cca_seen) {
  1161. /*
  1162. * From ACPI spec, OSPM will ignore _CCA if an ancestor
  1163. * already saw one.
  1164. */
  1165. adev->flags.cca_seen = 1;
  1166. cca = parent->flags.coherent_dma;
  1167. } else {
  1168. status = acpi_evaluate_integer(adev->handle, "_CCA",
  1169. NULL, &cca);
  1170. if (ACPI_SUCCESS(status))
  1171. adev->flags.cca_seen = 1;
  1172. else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1173. /*
  1174. * If architecture does not specify that _CCA is
  1175. * required for DMA-able devices (e.g. x86),
  1176. * we default to _CCA=1.
  1177. */
  1178. cca = 1;
  1179. else
  1180. acpi_handle_debug(adev->handle,
  1181. "ACPI device is missing _CCA.\n");
  1182. }
  1183. adev->flags.coherent_dma = cca;
  1184. }
  1185. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1186. int type, unsigned long long sta)
  1187. {
  1188. INIT_LIST_HEAD(&device->pnp.ids);
  1189. device->device_type = type;
  1190. device->handle = handle;
  1191. device->parent = acpi_bus_get_parent(handle);
  1192. device->fwnode.type = FWNODE_ACPI;
  1193. acpi_set_device_status(device, sta);
  1194. acpi_device_get_busid(device);
  1195. acpi_set_pnp_ids(handle, &device->pnp, type);
  1196. acpi_init_properties(device);
  1197. acpi_bus_get_flags(device);
  1198. device->flags.match_driver = false;
  1199. device->flags.initialized = true;
  1200. acpi_device_clear_enumerated(device);
  1201. device_initialize(&device->dev);
  1202. dev_set_uevent_suppress(&device->dev, true);
  1203. acpi_init_coherency(device);
  1204. }
  1205. void acpi_device_add_finalize(struct acpi_device *device)
  1206. {
  1207. dev_set_uevent_suppress(&device->dev, false);
  1208. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1209. }
  1210. static int acpi_add_single_object(struct acpi_device **child,
  1211. acpi_handle handle, int type,
  1212. unsigned long long sta)
  1213. {
  1214. int result;
  1215. struct acpi_device *device;
  1216. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1217. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1218. if (!device) {
  1219. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1220. return -ENOMEM;
  1221. }
  1222. acpi_init_device_object(device, handle, type, sta);
  1223. acpi_bus_get_power_flags(device);
  1224. acpi_bus_get_wakeup_device_flags(device);
  1225. result = acpi_device_add(device, acpi_device_release);
  1226. if (result) {
  1227. acpi_device_release(&device->dev);
  1228. return result;
  1229. }
  1230. acpi_power_add_remove_device(device, true);
  1231. acpi_device_add_finalize(device);
  1232. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1233. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1234. dev_name(&device->dev), (char *) buffer.pointer,
  1235. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1236. kfree(buffer.pointer);
  1237. *child = device;
  1238. return 0;
  1239. }
  1240. static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
  1241. void *context)
  1242. {
  1243. struct resource *res = context;
  1244. if (acpi_dev_resource_memory(ares, res))
  1245. return AE_CTRL_TERMINATE;
  1246. return AE_OK;
  1247. }
  1248. static bool acpi_device_should_be_hidden(acpi_handle handle)
  1249. {
  1250. acpi_status status;
  1251. struct resource res;
  1252. /* Check if it should ignore the UART device */
  1253. if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
  1254. return false;
  1255. /*
  1256. * The UART device described in SPCR table is assumed to have only one
  1257. * memory resource present. So we only look for the first one here.
  1258. */
  1259. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1260. acpi_get_resource_memory, &res);
  1261. if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
  1262. return false;
  1263. acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
  1264. &res.start);
  1265. return true;
  1266. }
  1267. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1268. unsigned long long *sta)
  1269. {
  1270. acpi_status status;
  1271. acpi_object_type acpi_type;
  1272. status = acpi_get_type(handle, &acpi_type);
  1273. if (ACPI_FAILURE(status))
  1274. return -ENODEV;
  1275. switch (acpi_type) {
  1276. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1277. case ACPI_TYPE_DEVICE:
  1278. if (acpi_device_should_be_hidden(handle))
  1279. return -ENODEV;
  1280. *type = ACPI_BUS_TYPE_DEVICE;
  1281. status = acpi_bus_get_status_handle(handle, sta);
  1282. if (ACPI_FAILURE(status))
  1283. *sta = 0;
  1284. break;
  1285. case ACPI_TYPE_PROCESSOR:
  1286. *type = ACPI_BUS_TYPE_PROCESSOR;
  1287. status = acpi_bus_get_status_handle(handle, sta);
  1288. if (ACPI_FAILURE(status))
  1289. return -ENODEV;
  1290. break;
  1291. case ACPI_TYPE_THERMAL:
  1292. *type = ACPI_BUS_TYPE_THERMAL;
  1293. *sta = ACPI_STA_DEFAULT;
  1294. break;
  1295. case ACPI_TYPE_POWER:
  1296. *type = ACPI_BUS_TYPE_POWER;
  1297. *sta = ACPI_STA_DEFAULT;
  1298. break;
  1299. default:
  1300. return -ENODEV;
  1301. }
  1302. return 0;
  1303. }
  1304. bool acpi_device_is_present(struct acpi_device *adev)
  1305. {
  1306. if (adev->status.present || adev->status.functional)
  1307. return true;
  1308. adev->flags.initialized = false;
  1309. return false;
  1310. }
  1311. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1312. const char *idstr,
  1313. const struct acpi_device_id **matchid)
  1314. {
  1315. const struct acpi_device_id *devid;
  1316. if (handler->match)
  1317. return handler->match(idstr, matchid);
  1318. for (devid = handler->ids; devid->id[0]; devid++)
  1319. if (!strcmp((char *)devid->id, idstr)) {
  1320. if (matchid)
  1321. *matchid = devid;
  1322. return true;
  1323. }
  1324. return false;
  1325. }
  1326. static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
  1327. const struct acpi_device_id **matchid)
  1328. {
  1329. struct acpi_scan_handler *handler;
  1330. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1331. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1332. return handler;
  1333. return NULL;
  1334. }
  1335. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1336. {
  1337. if (!!hotplug->enabled == !!val)
  1338. return;
  1339. mutex_lock(&acpi_scan_lock);
  1340. hotplug->enabled = val;
  1341. mutex_unlock(&acpi_scan_lock);
  1342. }
  1343. static void acpi_scan_init_hotplug(struct acpi_device *adev)
  1344. {
  1345. struct acpi_hardware_id *hwid;
  1346. if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
  1347. acpi_dock_add(adev);
  1348. return;
  1349. }
  1350. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  1351. struct acpi_scan_handler *handler;
  1352. handler = acpi_scan_match_handler(hwid->id, NULL);
  1353. if (handler) {
  1354. adev->flags.hotplug_notify = true;
  1355. break;
  1356. }
  1357. }
  1358. }
  1359. static void acpi_device_dep_initialize(struct acpi_device *adev)
  1360. {
  1361. struct acpi_dep_data *dep;
  1362. struct acpi_handle_list dep_devices;
  1363. acpi_status status;
  1364. int i;
  1365. if (!acpi_has_method(adev->handle, "_DEP"))
  1366. return;
  1367. status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
  1368. &dep_devices);
  1369. if (ACPI_FAILURE(status)) {
  1370. dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
  1371. return;
  1372. }
  1373. for (i = 0; i < dep_devices.count; i++) {
  1374. struct acpi_device_info *info;
  1375. int skip;
  1376. status = acpi_get_object_info(dep_devices.handles[i], &info);
  1377. if (ACPI_FAILURE(status)) {
  1378. dev_dbg(&adev->dev, "Error reading _DEP device info\n");
  1379. continue;
  1380. }
  1381. /*
  1382. * Skip the dependency of Windows System Power
  1383. * Management Controller
  1384. */
  1385. skip = info->valid & ACPI_VALID_HID &&
  1386. !strcmp(info->hardware_id.string, "INT3396");
  1387. kfree(info);
  1388. if (skip)
  1389. continue;
  1390. dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
  1391. if (!dep)
  1392. return;
  1393. dep->master = dep_devices.handles[i];
  1394. dep->slave = adev->handle;
  1395. adev->dep_unmet++;
  1396. mutex_lock(&acpi_dep_list_lock);
  1397. list_add_tail(&dep->node , &acpi_dep_list);
  1398. mutex_unlock(&acpi_dep_list_lock);
  1399. }
  1400. }
  1401. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1402. void *not_used, void **return_value)
  1403. {
  1404. struct acpi_device *device = NULL;
  1405. int type;
  1406. unsigned long long sta;
  1407. int result;
  1408. acpi_bus_get_device(handle, &device);
  1409. if (device)
  1410. goto out;
  1411. result = acpi_bus_type_and_status(handle, &type, &sta);
  1412. if (result)
  1413. return AE_OK;
  1414. if (type == ACPI_BUS_TYPE_POWER) {
  1415. acpi_add_power_resource(handle);
  1416. return AE_OK;
  1417. }
  1418. acpi_add_single_object(&device, handle, type, sta);
  1419. if (!device)
  1420. return AE_CTRL_DEPTH;
  1421. acpi_scan_init_hotplug(device);
  1422. acpi_device_dep_initialize(device);
  1423. out:
  1424. if (!*return_value)
  1425. *return_value = device;
  1426. return AE_OK;
  1427. }
  1428. static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
  1429. {
  1430. bool *is_spi_i2c_slave_p = data;
  1431. if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  1432. return 1;
  1433. /*
  1434. * devices that are connected to UART still need to be enumerated to
  1435. * platform bus
  1436. */
  1437. if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  1438. *is_spi_i2c_slave_p = true;
  1439. /* no need to do more checking */
  1440. return -1;
  1441. }
  1442. static void acpi_default_enumeration(struct acpi_device *device)
  1443. {
  1444. struct list_head resource_list;
  1445. bool is_spi_i2c_slave = false;
  1446. /*
  1447. * Do not enumerate SPI/I2C slaves as they will be enumerated by their
  1448. * respective parents.
  1449. */
  1450. INIT_LIST_HEAD(&resource_list);
  1451. acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
  1452. &is_spi_i2c_slave);
  1453. acpi_dev_free_resource_list(&resource_list);
  1454. if (!is_spi_i2c_slave) {
  1455. acpi_create_platform_device(device, NULL);
  1456. acpi_device_set_enumerated(device);
  1457. } else {
  1458. blocking_notifier_call_chain(&acpi_reconfig_chain,
  1459. ACPI_RECONFIG_DEVICE_ADD, device);
  1460. }
  1461. }
  1462. static const struct acpi_device_id generic_device_ids[] = {
  1463. {ACPI_DT_NAMESPACE_HID, },
  1464. {"", },
  1465. };
  1466. static int acpi_generic_device_attach(struct acpi_device *adev,
  1467. const struct acpi_device_id *not_used)
  1468. {
  1469. /*
  1470. * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
  1471. * below can be unconditional.
  1472. */
  1473. if (adev->data.of_compatible)
  1474. acpi_default_enumeration(adev);
  1475. return 1;
  1476. }
  1477. static struct acpi_scan_handler generic_device_handler = {
  1478. .ids = generic_device_ids,
  1479. .attach = acpi_generic_device_attach,
  1480. };
  1481. static int acpi_scan_attach_handler(struct acpi_device *device)
  1482. {
  1483. struct acpi_hardware_id *hwid;
  1484. int ret = 0;
  1485. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1486. const struct acpi_device_id *devid;
  1487. struct acpi_scan_handler *handler;
  1488. handler = acpi_scan_match_handler(hwid->id, &devid);
  1489. if (handler) {
  1490. if (!handler->attach) {
  1491. device->pnp.type.platform_id = 0;
  1492. continue;
  1493. }
  1494. device->handler = handler;
  1495. ret = handler->attach(device, devid);
  1496. if (ret > 0)
  1497. break;
  1498. device->handler = NULL;
  1499. if (ret < 0)
  1500. break;
  1501. }
  1502. }
  1503. return ret;
  1504. }
  1505. static void acpi_bus_attach(struct acpi_device *device)
  1506. {
  1507. struct acpi_device *child;
  1508. acpi_handle ejd;
  1509. int ret;
  1510. if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
  1511. register_dock_dependent_device(device, ejd);
  1512. acpi_bus_get_status(device);
  1513. /* Skip devices that are not present. */
  1514. if (!acpi_device_is_present(device)) {
  1515. acpi_device_clear_enumerated(device);
  1516. device->flags.power_manageable = 0;
  1517. return;
  1518. }
  1519. if (device->handler)
  1520. goto ok;
  1521. if (!device->flags.initialized) {
  1522. device->flags.power_manageable =
  1523. device->power.states[ACPI_STATE_D0].flags.valid;
  1524. if (acpi_bus_init_power(device))
  1525. device->flags.power_manageable = 0;
  1526. device->flags.initialized = true;
  1527. }
  1528. ret = acpi_scan_attach_handler(device);
  1529. if (ret < 0)
  1530. return;
  1531. device->flags.match_driver = true;
  1532. if (!ret) {
  1533. ret = device_attach(&device->dev);
  1534. if (ret < 0)
  1535. return;
  1536. if (!ret && device->pnp.type.platform_id)
  1537. acpi_default_enumeration(device);
  1538. }
  1539. ok:
  1540. list_for_each_entry(child, &device->children, node)
  1541. acpi_bus_attach(child);
  1542. if (device->handler && device->handler->hotplug.notify_online)
  1543. device->handler->hotplug.notify_online(device);
  1544. }
  1545. void acpi_walk_dep_device_list(acpi_handle handle)
  1546. {
  1547. struct acpi_dep_data *dep, *tmp;
  1548. struct acpi_device *adev;
  1549. mutex_lock(&acpi_dep_list_lock);
  1550. list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
  1551. if (dep->master == handle) {
  1552. acpi_bus_get_device(dep->slave, &adev);
  1553. if (!adev)
  1554. continue;
  1555. adev->dep_unmet--;
  1556. if (!adev->dep_unmet)
  1557. acpi_bus_attach(adev);
  1558. list_del(&dep->node);
  1559. kfree(dep);
  1560. }
  1561. }
  1562. mutex_unlock(&acpi_dep_list_lock);
  1563. }
  1564. EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
  1565. /**
  1566. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1567. * @handle: Root of the namespace scope to scan.
  1568. *
  1569. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1570. * found devices.
  1571. *
  1572. * If no devices were found, -ENODEV is returned, but it does not mean that
  1573. * there has been a real error. There just have been no suitable ACPI objects
  1574. * in the table trunk from which the kernel could create a device and add an
  1575. * appropriate driver.
  1576. *
  1577. * Must be called under acpi_scan_lock.
  1578. */
  1579. int acpi_bus_scan(acpi_handle handle)
  1580. {
  1581. void *device = NULL;
  1582. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1583. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1584. acpi_bus_check_add, NULL, NULL, &device);
  1585. if (device) {
  1586. acpi_bus_attach(device);
  1587. return 0;
  1588. }
  1589. return -ENODEV;
  1590. }
  1591. EXPORT_SYMBOL(acpi_bus_scan);
  1592. /**
  1593. * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
  1594. * @adev: Root of the ACPI namespace scope to walk.
  1595. *
  1596. * Must be called under acpi_scan_lock.
  1597. */
  1598. void acpi_bus_trim(struct acpi_device *adev)
  1599. {
  1600. struct acpi_scan_handler *handler = adev->handler;
  1601. struct acpi_device *child;
  1602. list_for_each_entry_reverse(child, &adev->children, node)
  1603. acpi_bus_trim(child);
  1604. adev->flags.match_driver = false;
  1605. if (handler) {
  1606. if (handler->detach)
  1607. handler->detach(adev);
  1608. adev->handler = NULL;
  1609. } else {
  1610. device_release_driver(&adev->dev);
  1611. }
  1612. /*
  1613. * Most likely, the device is going away, so put it into D3cold before
  1614. * that.
  1615. */
  1616. acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
  1617. adev->flags.initialized = false;
  1618. acpi_device_clear_enumerated(adev);
  1619. }
  1620. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1621. static int acpi_bus_scan_fixed(void)
  1622. {
  1623. int result = 0;
  1624. /*
  1625. * Enumerate all fixed-feature devices.
  1626. */
  1627. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1628. struct acpi_device *device = NULL;
  1629. result = acpi_add_single_object(&device, NULL,
  1630. ACPI_BUS_TYPE_POWER_BUTTON,
  1631. ACPI_STA_DEFAULT);
  1632. if (result)
  1633. return result;
  1634. device->flags.match_driver = true;
  1635. result = device_attach(&device->dev);
  1636. if (result < 0)
  1637. return result;
  1638. device_init_wakeup(&device->dev, true);
  1639. }
  1640. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1641. struct acpi_device *device = NULL;
  1642. result = acpi_add_single_object(&device, NULL,
  1643. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1644. ACPI_STA_DEFAULT);
  1645. if (result)
  1646. return result;
  1647. device->flags.match_driver = true;
  1648. result = device_attach(&device->dev);
  1649. }
  1650. return result < 0 ? result : 0;
  1651. }
  1652. static void __init acpi_get_spcr_uart_addr(void)
  1653. {
  1654. acpi_status status;
  1655. struct acpi_table_spcr *spcr_ptr;
  1656. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  1657. (struct acpi_table_header **)&spcr_ptr);
  1658. if (ACPI_SUCCESS(status))
  1659. spcr_uart_addr = spcr_ptr->serial_port.address;
  1660. else
  1661. printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
  1662. }
  1663. static bool acpi_scan_initialized;
  1664. int __init acpi_scan_init(void)
  1665. {
  1666. int result;
  1667. acpi_status status;
  1668. struct acpi_table_stao *stao_ptr;
  1669. acpi_pci_root_init();
  1670. acpi_pci_link_init();
  1671. acpi_processor_init();
  1672. acpi_lpss_init();
  1673. acpi_apd_init();
  1674. acpi_cmos_rtc_init();
  1675. acpi_container_init();
  1676. acpi_memory_hotplug_init();
  1677. acpi_pnp_init();
  1678. acpi_int340x_thermal_init();
  1679. acpi_amba_init();
  1680. acpi_watchdog_init();
  1681. acpi_scan_add_handler(&generic_device_handler);
  1682. /*
  1683. * If there is STAO table, check whether it needs to ignore the UART
  1684. * device in SPCR table.
  1685. */
  1686. status = acpi_get_table(ACPI_SIG_STAO, 0,
  1687. (struct acpi_table_header **)&stao_ptr);
  1688. if (ACPI_SUCCESS(status)) {
  1689. if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
  1690. printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
  1691. if (stao_ptr->ignore_uart)
  1692. acpi_get_spcr_uart_addr();
  1693. }
  1694. mutex_lock(&acpi_scan_lock);
  1695. /*
  1696. * Enumerate devices in the ACPI namespace.
  1697. */
  1698. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1699. if (result)
  1700. goto out;
  1701. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1702. if (result)
  1703. goto out;
  1704. /* Fixed feature devices do not exist on HW-reduced platform */
  1705. if (!acpi_gbl_reduced_hardware) {
  1706. result = acpi_bus_scan_fixed();
  1707. if (result) {
  1708. acpi_detach_data(acpi_root->handle,
  1709. acpi_scan_drop_device);
  1710. acpi_device_del(acpi_root);
  1711. put_device(&acpi_root->dev);
  1712. goto out;
  1713. }
  1714. }
  1715. acpi_update_all_gpes();
  1716. acpi_ec_ecdt_start();
  1717. acpi_scan_initialized = true;
  1718. out:
  1719. mutex_unlock(&acpi_scan_lock);
  1720. return result;
  1721. }
  1722. static struct acpi_probe_entry *ape;
  1723. static int acpi_probe_count;
  1724. static DEFINE_MUTEX(acpi_probe_mutex);
  1725. static int __init acpi_match_madt(struct acpi_subtable_header *header,
  1726. const unsigned long end)
  1727. {
  1728. if (!ape->subtable_valid || ape->subtable_valid(header, ape))
  1729. if (!ape->probe_subtbl(header, end))
  1730. acpi_probe_count++;
  1731. return 0;
  1732. }
  1733. int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
  1734. {
  1735. int count = 0;
  1736. if (acpi_disabled)
  1737. return 0;
  1738. mutex_lock(&acpi_probe_mutex);
  1739. for (ape = ap_head; nr; ape++, nr--) {
  1740. if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
  1741. acpi_probe_count = 0;
  1742. acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
  1743. count += acpi_probe_count;
  1744. } else {
  1745. int res;
  1746. res = acpi_table_parse(ape->id, ape->probe_table);
  1747. if (!res)
  1748. count++;
  1749. }
  1750. }
  1751. mutex_unlock(&acpi_probe_mutex);
  1752. return count;
  1753. }
  1754. struct acpi_table_events_work {
  1755. struct work_struct work;
  1756. void *table;
  1757. u32 event;
  1758. };
  1759. static void acpi_table_events_fn(struct work_struct *work)
  1760. {
  1761. struct acpi_table_events_work *tew;
  1762. tew = container_of(work, struct acpi_table_events_work, work);
  1763. if (tew->event == ACPI_TABLE_EVENT_LOAD) {
  1764. acpi_scan_lock_acquire();
  1765. acpi_bus_scan(ACPI_ROOT_OBJECT);
  1766. acpi_scan_lock_release();
  1767. }
  1768. kfree(tew);
  1769. }
  1770. void acpi_scan_table_handler(u32 event, void *table, void *context)
  1771. {
  1772. struct acpi_table_events_work *tew;
  1773. if (!acpi_scan_initialized)
  1774. return;
  1775. if (event != ACPI_TABLE_EVENT_LOAD)
  1776. return;
  1777. tew = kmalloc(sizeof(*tew), GFP_KERNEL);
  1778. if (!tew)
  1779. return;
  1780. INIT_WORK(&tew->work, acpi_table_events_fn);
  1781. tew->table = table;
  1782. tew->event = event;
  1783. schedule_work(&tew->work);
  1784. }
  1785. int acpi_reconfig_notifier_register(struct notifier_block *nb)
  1786. {
  1787. return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
  1788. }
  1789. EXPORT_SYMBOL(acpi_reconfig_notifier_register);
  1790. int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
  1791. {
  1792. return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
  1793. }
  1794. EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);