scan.c 54 KB

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