scan.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  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) && PTR_ERR(iommu) == -EPROBE_DEFER)
  1160. return -EPROBE_DEFER;
  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. static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
  1210. {
  1211. bool *is_spi_i2c_slave_p = data;
  1212. if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  1213. return 1;
  1214. /*
  1215. * devices that are connected to UART still need to be enumerated to
  1216. * platform bus
  1217. */
  1218. if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  1219. *is_spi_i2c_slave_p = true;
  1220. /* no need to do more checking */
  1221. return -1;
  1222. }
  1223. static bool acpi_is_spi_i2c_slave(struct acpi_device *device)
  1224. {
  1225. struct list_head resource_list;
  1226. bool is_spi_i2c_slave = false;
  1227. INIT_LIST_HEAD(&resource_list);
  1228. acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
  1229. &is_spi_i2c_slave);
  1230. acpi_dev_free_resource_list(&resource_list);
  1231. return is_spi_i2c_slave;
  1232. }
  1233. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1234. int type, unsigned long long sta)
  1235. {
  1236. INIT_LIST_HEAD(&device->pnp.ids);
  1237. device->device_type = type;
  1238. device->handle = handle;
  1239. device->parent = acpi_bus_get_parent(handle);
  1240. device->fwnode.type = FWNODE_ACPI;
  1241. acpi_set_device_status(device, sta);
  1242. acpi_device_get_busid(device);
  1243. acpi_set_pnp_ids(handle, &device->pnp, type);
  1244. acpi_init_properties(device);
  1245. acpi_bus_get_flags(device);
  1246. device->flags.match_driver = false;
  1247. device->flags.initialized = true;
  1248. device->flags.spi_i2c_slave = acpi_is_spi_i2c_slave(device);
  1249. acpi_device_clear_enumerated(device);
  1250. device_initialize(&device->dev);
  1251. dev_set_uevent_suppress(&device->dev, true);
  1252. acpi_init_coherency(device);
  1253. }
  1254. void acpi_device_add_finalize(struct acpi_device *device)
  1255. {
  1256. dev_set_uevent_suppress(&device->dev, false);
  1257. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1258. }
  1259. static int acpi_add_single_object(struct acpi_device **child,
  1260. acpi_handle handle, int type,
  1261. unsigned long long sta)
  1262. {
  1263. int result;
  1264. struct acpi_device *device;
  1265. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1266. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1267. if (!device) {
  1268. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1269. return -ENOMEM;
  1270. }
  1271. acpi_init_device_object(device, handle, type, sta);
  1272. acpi_bus_get_power_flags(device);
  1273. acpi_bus_get_wakeup_device_flags(device);
  1274. result = acpi_device_add(device, acpi_device_release);
  1275. if (result) {
  1276. acpi_device_release(&device->dev);
  1277. return result;
  1278. }
  1279. acpi_power_add_remove_device(device, true);
  1280. acpi_device_add_finalize(device);
  1281. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1282. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1283. dev_name(&device->dev), (char *) buffer.pointer,
  1284. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1285. kfree(buffer.pointer);
  1286. *child = device;
  1287. return 0;
  1288. }
  1289. static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
  1290. void *context)
  1291. {
  1292. struct resource *res = context;
  1293. if (acpi_dev_resource_memory(ares, res))
  1294. return AE_CTRL_TERMINATE;
  1295. return AE_OK;
  1296. }
  1297. static bool acpi_device_should_be_hidden(acpi_handle handle)
  1298. {
  1299. acpi_status status;
  1300. struct resource res;
  1301. /* Check if it should ignore the UART device */
  1302. if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
  1303. return false;
  1304. /*
  1305. * The UART device described in SPCR table is assumed to have only one
  1306. * memory resource present. So we only look for the first one here.
  1307. */
  1308. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1309. acpi_get_resource_memory, &res);
  1310. if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
  1311. return false;
  1312. acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
  1313. &res.start);
  1314. return true;
  1315. }
  1316. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1317. unsigned long long *sta)
  1318. {
  1319. acpi_status status;
  1320. acpi_object_type acpi_type;
  1321. status = acpi_get_type(handle, &acpi_type);
  1322. if (ACPI_FAILURE(status))
  1323. return -ENODEV;
  1324. switch (acpi_type) {
  1325. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1326. case ACPI_TYPE_DEVICE:
  1327. if (acpi_device_should_be_hidden(handle))
  1328. return -ENODEV;
  1329. *type = ACPI_BUS_TYPE_DEVICE;
  1330. status = acpi_bus_get_status_handle(handle, sta);
  1331. if (ACPI_FAILURE(status))
  1332. *sta = 0;
  1333. break;
  1334. case ACPI_TYPE_PROCESSOR:
  1335. *type = ACPI_BUS_TYPE_PROCESSOR;
  1336. status = acpi_bus_get_status_handle(handle, sta);
  1337. if (ACPI_FAILURE(status))
  1338. return -ENODEV;
  1339. break;
  1340. case ACPI_TYPE_THERMAL:
  1341. *type = ACPI_BUS_TYPE_THERMAL;
  1342. *sta = ACPI_STA_DEFAULT;
  1343. break;
  1344. case ACPI_TYPE_POWER:
  1345. *type = ACPI_BUS_TYPE_POWER;
  1346. *sta = ACPI_STA_DEFAULT;
  1347. break;
  1348. default:
  1349. return -ENODEV;
  1350. }
  1351. return 0;
  1352. }
  1353. bool acpi_device_is_present(struct acpi_device *adev)
  1354. {
  1355. if (adev->status.present || adev->status.functional)
  1356. return true;
  1357. adev->flags.initialized = false;
  1358. return false;
  1359. }
  1360. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1361. const char *idstr,
  1362. const struct acpi_device_id **matchid)
  1363. {
  1364. const struct acpi_device_id *devid;
  1365. if (handler->match)
  1366. return handler->match(idstr, matchid);
  1367. for (devid = handler->ids; devid->id[0]; devid++)
  1368. if (!strcmp((char *)devid->id, idstr)) {
  1369. if (matchid)
  1370. *matchid = devid;
  1371. return true;
  1372. }
  1373. return false;
  1374. }
  1375. static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
  1376. const struct acpi_device_id **matchid)
  1377. {
  1378. struct acpi_scan_handler *handler;
  1379. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1380. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1381. return handler;
  1382. return NULL;
  1383. }
  1384. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1385. {
  1386. if (!!hotplug->enabled == !!val)
  1387. return;
  1388. mutex_lock(&acpi_scan_lock);
  1389. hotplug->enabled = val;
  1390. mutex_unlock(&acpi_scan_lock);
  1391. }
  1392. static void acpi_scan_init_hotplug(struct acpi_device *adev)
  1393. {
  1394. struct acpi_hardware_id *hwid;
  1395. if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
  1396. acpi_dock_add(adev);
  1397. return;
  1398. }
  1399. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  1400. struct acpi_scan_handler *handler;
  1401. handler = acpi_scan_match_handler(hwid->id, NULL);
  1402. if (handler) {
  1403. adev->flags.hotplug_notify = true;
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. static void acpi_device_dep_initialize(struct acpi_device *adev)
  1409. {
  1410. struct acpi_dep_data *dep;
  1411. struct acpi_handle_list dep_devices;
  1412. acpi_status status;
  1413. int i;
  1414. if (!acpi_has_method(adev->handle, "_DEP"))
  1415. return;
  1416. status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
  1417. &dep_devices);
  1418. if (ACPI_FAILURE(status)) {
  1419. dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
  1420. return;
  1421. }
  1422. for (i = 0; i < dep_devices.count; i++) {
  1423. struct acpi_device_info *info;
  1424. int skip;
  1425. status = acpi_get_object_info(dep_devices.handles[i], &info);
  1426. if (ACPI_FAILURE(status)) {
  1427. dev_dbg(&adev->dev, "Error reading _DEP device info\n");
  1428. continue;
  1429. }
  1430. /*
  1431. * Skip the dependency of Windows System Power
  1432. * Management Controller
  1433. */
  1434. skip = info->valid & ACPI_VALID_HID &&
  1435. !strcmp(info->hardware_id.string, "INT3396");
  1436. kfree(info);
  1437. if (skip)
  1438. continue;
  1439. dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
  1440. if (!dep)
  1441. return;
  1442. dep->master = dep_devices.handles[i];
  1443. dep->slave = adev->handle;
  1444. adev->dep_unmet++;
  1445. mutex_lock(&acpi_dep_list_lock);
  1446. list_add_tail(&dep->node , &acpi_dep_list);
  1447. mutex_unlock(&acpi_dep_list_lock);
  1448. }
  1449. }
  1450. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1451. void *not_used, void **return_value)
  1452. {
  1453. struct acpi_device *device = NULL;
  1454. int type;
  1455. unsigned long long sta;
  1456. int result;
  1457. acpi_bus_get_device(handle, &device);
  1458. if (device)
  1459. goto out;
  1460. result = acpi_bus_type_and_status(handle, &type, &sta);
  1461. if (result)
  1462. return AE_OK;
  1463. if (type == ACPI_BUS_TYPE_POWER) {
  1464. acpi_add_power_resource(handle);
  1465. return AE_OK;
  1466. }
  1467. acpi_add_single_object(&device, handle, type, sta);
  1468. if (!device)
  1469. return AE_CTRL_DEPTH;
  1470. acpi_scan_init_hotplug(device);
  1471. acpi_device_dep_initialize(device);
  1472. out:
  1473. if (!*return_value)
  1474. *return_value = device;
  1475. return AE_OK;
  1476. }
  1477. static void acpi_default_enumeration(struct acpi_device *device)
  1478. {
  1479. /*
  1480. * Do not enumerate SPI/I2C slaves as they will be enumerated by their
  1481. * respective parents.
  1482. */
  1483. if (!device->flags.spi_i2c_slave) {
  1484. acpi_create_platform_device(device, NULL);
  1485. acpi_device_set_enumerated(device);
  1486. } else {
  1487. blocking_notifier_call_chain(&acpi_reconfig_chain,
  1488. ACPI_RECONFIG_DEVICE_ADD, device);
  1489. }
  1490. }
  1491. static const struct acpi_device_id generic_device_ids[] = {
  1492. {ACPI_DT_NAMESPACE_HID, },
  1493. {"", },
  1494. };
  1495. static int acpi_generic_device_attach(struct acpi_device *adev,
  1496. const struct acpi_device_id *not_used)
  1497. {
  1498. /*
  1499. * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
  1500. * below can be unconditional.
  1501. */
  1502. if (adev->data.of_compatible)
  1503. acpi_default_enumeration(adev);
  1504. return 1;
  1505. }
  1506. static struct acpi_scan_handler generic_device_handler = {
  1507. .ids = generic_device_ids,
  1508. .attach = acpi_generic_device_attach,
  1509. };
  1510. static int acpi_scan_attach_handler(struct acpi_device *device)
  1511. {
  1512. struct acpi_hardware_id *hwid;
  1513. int ret = 0;
  1514. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1515. const struct acpi_device_id *devid;
  1516. struct acpi_scan_handler *handler;
  1517. handler = acpi_scan_match_handler(hwid->id, &devid);
  1518. if (handler) {
  1519. if (!handler->attach) {
  1520. device->pnp.type.platform_id = 0;
  1521. continue;
  1522. }
  1523. device->handler = handler;
  1524. ret = handler->attach(device, devid);
  1525. if (ret > 0)
  1526. break;
  1527. device->handler = NULL;
  1528. if (ret < 0)
  1529. break;
  1530. }
  1531. }
  1532. return ret;
  1533. }
  1534. static void acpi_bus_attach(struct acpi_device *device)
  1535. {
  1536. struct acpi_device *child;
  1537. acpi_handle ejd;
  1538. int ret;
  1539. if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
  1540. register_dock_dependent_device(device, ejd);
  1541. acpi_bus_get_status(device);
  1542. /* Skip devices that are not present. */
  1543. if (!acpi_device_is_present(device)) {
  1544. acpi_device_clear_enumerated(device);
  1545. device->flags.power_manageable = 0;
  1546. return;
  1547. }
  1548. if (device->handler)
  1549. goto ok;
  1550. if (!device->flags.initialized) {
  1551. device->flags.power_manageable =
  1552. device->power.states[ACPI_STATE_D0].flags.valid;
  1553. if (acpi_bus_init_power(device))
  1554. device->flags.power_manageable = 0;
  1555. device->flags.initialized = true;
  1556. } else if (device->flags.visited) {
  1557. goto ok;
  1558. }
  1559. ret = acpi_scan_attach_handler(device);
  1560. if (ret < 0)
  1561. return;
  1562. device->flags.match_driver = true;
  1563. if (ret > 0 && !device->flags.spi_i2c_slave) {
  1564. acpi_device_set_enumerated(device);
  1565. goto ok;
  1566. }
  1567. ret = device_attach(&device->dev);
  1568. if (ret < 0)
  1569. return;
  1570. if (!device->pnp.type.platform_id && !device->flags.spi_i2c_slave)
  1571. acpi_device_set_enumerated(device);
  1572. else
  1573. acpi_default_enumeration(device);
  1574. ok:
  1575. list_for_each_entry(child, &device->children, node)
  1576. acpi_bus_attach(child);
  1577. if (device->handler && device->handler->hotplug.notify_online)
  1578. device->handler->hotplug.notify_online(device);
  1579. }
  1580. void acpi_walk_dep_device_list(acpi_handle handle)
  1581. {
  1582. struct acpi_dep_data *dep, *tmp;
  1583. struct acpi_device *adev;
  1584. mutex_lock(&acpi_dep_list_lock);
  1585. list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
  1586. if (dep->master == handle) {
  1587. acpi_bus_get_device(dep->slave, &adev);
  1588. if (!adev)
  1589. continue;
  1590. adev->dep_unmet--;
  1591. if (!adev->dep_unmet)
  1592. acpi_bus_attach(adev);
  1593. list_del(&dep->node);
  1594. kfree(dep);
  1595. }
  1596. }
  1597. mutex_unlock(&acpi_dep_list_lock);
  1598. }
  1599. EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
  1600. /**
  1601. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1602. * @handle: Root of the namespace scope to scan.
  1603. *
  1604. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1605. * found devices.
  1606. *
  1607. * If no devices were found, -ENODEV is returned, but it does not mean that
  1608. * there has been a real error. There just have been no suitable ACPI objects
  1609. * in the table trunk from which the kernel could create a device and add an
  1610. * appropriate driver.
  1611. *
  1612. * Must be called under acpi_scan_lock.
  1613. */
  1614. int acpi_bus_scan(acpi_handle handle)
  1615. {
  1616. void *device = NULL;
  1617. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1618. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1619. acpi_bus_check_add, NULL, NULL, &device);
  1620. if (device) {
  1621. acpi_bus_attach(device);
  1622. return 0;
  1623. }
  1624. return -ENODEV;
  1625. }
  1626. EXPORT_SYMBOL(acpi_bus_scan);
  1627. /**
  1628. * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
  1629. * @adev: Root of the ACPI namespace scope to walk.
  1630. *
  1631. * Must be called under acpi_scan_lock.
  1632. */
  1633. void acpi_bus_trim(struct acpi_device *adev)
  1634. {
  1635. struct acpi_scan_handler *handler = adev->handler;
  1636. struct acpi_device *child;
  1637. list_for_each_entry_reverse(child, &adev->children, node)
  1638. acpi_bus_trim(child);
  1639. adev->flags.match_driver = false;
  1640. if (handler) {
  1641. if (handler->detach)
  1642. handler->detach(adev);
  1643. adev->handler = NULL;
  1644. } else {
  1645. device_release_driver(&adev->dev);
  1646. }
  1647. /*
  1648. * Most likely, the device is going away, so put it into D3cold before
  1649. * that.
  1650. */
  1651. acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
  1652. adev->flags.initialized = false;
  1653. acpi_device_clear_enumerated(adev);
  1654. }
  1655. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1656. static int acpi_bus_scan_fixed(void)
  1657. {
  1658. int result = 0;
  1659. /*
  1660. * Enumerate all fixed-feature devices.
  1661. */
  1662. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1663. struct acpi_device *device = NULL;
  1664. result = acpi_add_single_object(&device, NULL,
  1665. ACPI_BUS_TYPE_POWER_BUTTON,
  1666. ACPI_STA_DEFAULT);
  1667. if (result)
  1668. return result;
  1669. device->flags.match_driver = true;
  1670. result = device_attach(&device->dev);
  1671. if (result < 0)
  1672. return result;
  1673. device_init_wakeup(&device->dev, true);
  1674. }
  1675. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1676. struct acpi_device *device = NULL;
  1677. result = acpi_add_single_object(&device, NULL,
  1678. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1679. ACPI_STA_DEFAULT);
  1680. if (result)
  1681. return result;
  1682. device->flags.match_driver = true;
  1683. result = device_attach(&device->dev);
  1684. }
  1685. return result < 0 ? result : 0;
  1686. }
  1687. static void __init acpi_get_spcr_uart_addr(void)
  1688. {
  1689. acpi_status status;
  1690. struct acpi_table_spcr *spcr_ptr;
  1691. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  1692. (struct acpi_table_header **)&spcr_ptr);
  1693. if (ACPI_SUCCESS(status))
  1694. spcr_uart_addr = spcr_ptr->serial_port.address;
  1695. else
  1696. printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
  1697. }
  1698. static bool acpi_scan_initialized;
  1699. int __init acpi_scan_init(void)
  1700. {
  1701. int result;
  1702. acpi_status status;
  1703. struct acpi_table_stao *stao_ptr;
  1704. acpi_pci_root_init();
  1705. acpi_pci_link_init();
  1706. acpi_processor_init();
  1707. acpi_lpss_init();
  1708. acpi_apd_init();
  1709. acpi_cmos_rtc_init();
  1710. acpi_container_init();
  1711. acpi_memory_hotplug_init();
  1712. acpi_pnp_init();
  1713. acpi_int340x_thermal_init();
  1714. acpi_amba_init();
  1715. acpi_watchdog_init();
  1716. acpi_scan_add_handler(&generic_device_handler);
  1717. /*
  1718. * If there is STAO table, check whether it needs to ignore the UART
  1719. * device in SPCR table.
  1720. */
  1721. status = acpi_get_table(ACPI_SIG_STAO, 0,
  1722. (struct acpi_table_header **)&stao_ptr);
  1723. if (ACPI_SUCCESS(status)) {
  1724. if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
  1725. printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
  1726. if (stao_ptr->ignore_uart)
  1727. acpi_get_spcr_uart_addr();
  1728. }
  1729. mutex_lock(&acpi_scan_lock);
  1730. /*
  1731. * Enumerate devices in the ACPI namespace.
  1732. */
  1733. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1734. if (result)
  1735. goto out;
  1736. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1737. if (result)
  1738. goto out;
  1739. /* Fixed feature devices do not exist on HW-reduced platform */
  1740. if (!acpi_gbl_reduced_hardware) {
  1741. result = acpi_bus_scan_fixed();
  1742. if (result) {
  1743. acpi_detach_data(acpi_root->handle,
  1744. acpi_scan_drop_device);
  1745. acpi_device_del(acpi_root);
  1746. put_device(&acpi_root->dev);
  1747. goto out;
  1748. }
  1749. }
  1750. acpi_gpe_apply_masked_gpes();
  1751. acpi_update_all_gpes();
  1752. acpi_ec_ecdt_start();
  1753. acpi_scan_initialized = true;
  1754. out:
  1755. mutex_unlock(&acpi_scan_lock);
  1756. return result;
  1757. }
  1758. static struct acpi_probe_entry *ape;
  1759. static int acpi_probe_count;
  1760. static DEFINE_MUTEX(acpi_probe_mutex);
  1761. static int __init acpi_match_madt(struct acpi_subtable_header *header,
  1762. const unsigned long end)
  1763. {
  1764. if (!ape->subtable_valid || ape->subtable_valid(header, ape))
  1765. if (!ape->probe_subtbl(header, end))
  1766. acpi_probe_count++;
  1767. return 0;
  1768. }
  1769. int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
  1770. {
  1771. int count = 0;
  1772. if (acpi_disabled)
  1773. return 0;
  1774. mutex_lock(&acpi_probe_mutex);
  1775. for (ape = ap_head; nr; ape++, nr--) {
  1776. if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
  1777. acpi_probe_count = 0;
  1778. acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
  1779. count += acpi_probe_count;
  1780. } else {
  1781. int res;
  1782. res = acpi_table_parse(ape->id, ape->probe_table);
  1783. if (!res)
  1784. count++;
  1785. }
  1786. }
  1787. mutex_unlock(&acpi_probe_mutex);
  1788. return count;
  1789. }
  1790. struct acpi_table_events_work {
  1791. struct work_struct work;
  1792. void *table;
  1793. u32 event;
  1794. };
  1795. static void acpi_table_events_fn(struct work_struct *work)
  1796. {
  1797. struct acpi_table_events_work *tew;
  1798. tew = container_of(work, struct acpi_table_events_work, work);
  1799. if (tew->event == ACPI_TABLE_EVENT_LOAD) {
  1800. acpi_scan_lock_acquire();
  1801. acpi_bus_scan(ACPI_ROOT_OBJECT);
  1802. acpi_scan_lock_release();
  1803. }
  1804. kfree(tew);
  1805. }
  1806. void acpi_scan_table_handler(u32 event, void *table, void *context)
  1807. {
  1808. struct acpi_table_events_work *tew;
  1809. if (!acpi_scan_initialized)
  1810. return;
  1811. if (event != ACPI_TABLE_EVENT_LOAD)
  1812. return;
  1813. tew = kmalloc(sizeof(*tew), GFP_KERNEL);
  1814. if (!tew)
  1815. return;
  1816. INIT_WORK(&tew->work, acpi_table_events_fn);
  1817. tew->table = table;
  1818. tew->event = event;
  1819. schedule_work(&tew->work);
  1820. }
  1821. int acpi_reconfig_notifier_register(struct notifier_block *nb)
  1822. {
  1823. return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
  1824. }
  1825. EXPORT_SYMBOL(acpi_reconfig_notifier_register);
  1826. int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
  1827. {
  1828. return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
  1829. }
  1830. EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);