scan.c 54 KB

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