scan.c 58 KB

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