scan.c 53 KB

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