scan.c 54 KB

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