scan.c 60 KB

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