scan.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  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. {"INT33FE", },
  1308. {}
  1309. };
  1310. if (acpi_is_indirect_io_slave(device))
  1311. return true;
  1312. /* Macs use device properties in lieu of _CRS resources */
  1313. if (x86_apple_machine &&
  1314. (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
  1315. fwnode_property_present(&device->fwnode, "i2cAddress") ||
  1316. fwnode_property_present(&device->fwnode, "baud")))
  1317. return true;
  1318. /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
  1319. if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
  1320. return false;
  1321. INIT_LIST_HEAD(&resource_list);
  1322. acpi_dev_get_resources(device, &resource_list,
  1323. acpi_check_serial_bus_slave,
  1324. &is_serial_bus_slave);
  1325. acpi_dev_free_resource_list(&resource_list);
  1326. return is_serial_bus_slave;
  1327. }
  1328. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1329. int type, unsigned long long sta)
  1330. {
  1331. INIT_LIST_HEAD(&device->pnp.ids);
  1332. device->device_type = type;
  1333. device->handle = handle;
  1334. device->parent = acpi_bus_get_parent(handle);
  1335. device->fwnode.ops = &acpi_device_fwnode_ops;
  1336. acpi_set_device_status(device, sta);
  1337. acpi_device_get_busid(device);
  1338. acpi_set_pnp_ids(handle, &device->pnp, type);
  1339. acpi_init_properties(device);
  1340. acpi_bus_get_flags(device);
  1341. device->flags.match_driver = false;
  1342. device->flags.initialized = true;
  1343. device->flags.enumeration_by_parent =
  1344. acpi_device_enumeration_by_parent(device);
  1345. acpi_device_clear_enumerated(device);
  1346. device_initialize(&device->dev);
  1347. dev_set_uevent_suppress(&device->dev, true);
  1348. acpi_init_coherency(device);
  1349. /* Assume there are unmet deps until acpi_device_dep_initialize() runs */
  1350. device->dep_unmet = 1;
  1351. }
  1352. void acpi_device_add_finalize(struct acpi_device *device)
  1353. {
  1354. dev_set_uevent_suppress(&device->dev, false);
  1355. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1356. }
  1357. static int acpi_add_single_object(struct acpi_device **child,
  1358. acpi_handle handle, int type,
  1359. unsigned long long sta)
  1360. {
  1361. int result;
  1362. struct acpi_device *device;
  1363. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1364. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1365. if (!device) {
  1366. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1367. return -ENOMEM;
  1368. }
  1369. acpi_init_device_object(device, handle, type, sta);
  1370. /*
  1371. * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
  1372. * that we can call acpi_bus_get_status() and use its quirk handling.
  1373. * Note this must be done before the get power-/wakeup_dev-flags calls.
  1374. */
  1375. if (type == ACPI_BUS_TYPE_DEVICE)
  1376. if (acpi_bus_get_status(device) < 0)
  1377. acpi_set_device_status(device, 0);
  1378. acpi_bus_get_power_flags(device);
  1379. acpi_bus_get_wakeup_device_flags(device);
  1380. result = acpi_device_add(device, acpi_device_release);
  1381. if (result) {
  1382. acpi_device_release(&device->dev);
  1383. return result;
  1384. }
  1385. acpi_power_add_remove_device(device, true);
  1386. acpi_device_add_finalize(device);
  1387. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1388. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1389. dev_name(&device->dev), (char *) buffer.pointer,
  1390. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1391. kfree(buffer.pointer);
  1392. *child = device;
  1393. return 0;
  1394. }
  1395. static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
  1396. void *context)
  1397. {
  1398. struct resource *res = context;
  1399. if (acpi_dev_resource_memory(ares, res))
  1400. return AE_CTRL_TERMINATE;
  1401. return AE_OK;
  1402. }
  1403. static bool acpi_device_should_be_hidden(acpi_handle handle)
  1404. {
  1405. acpi_status status;
  1406. struct resource res;
  1407. /* Check if it should ignore the UART device */
  1408. if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
  1409. return false;
  1410. /*
  1411. * The UART device described in SPCR table is assumed to have only one
  1412. * memory resource present. So we only look for the first one here.
  1413. */
  1414. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1415. acpi_get_resource_memory, &res);
  1416. if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
  1417. return false;
  1418. acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
  1419. &res.start);
  1420. return true;
  1421. }
  1422. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1423. unsigned long long *sta)
  1424. {
  1425. acpi_status status;
  1426. acpi_object_type acpi_type;
  1427. status = acpi_get_type(handle, &acpi_type);
  1428. if (ACPI_FAILURE(status))
  1429. return -ENODEV;
  1430. switch (acpi_type) {
  1431. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1432. case ACPI_TYPE_DEVICE:
  1433. if (acpi_device_should_be_hidden(handle))
  1434. return -ENODEV;
  1435. *type = ACPI_BUS_TYPE_DEVICE;
  1436. /*
  1437. * acpi_add_single_object updates this once we've an acpi_device
  1438. * so that acpi_bus_get_status' quirk handling can be used.
  1439. */
  1440. *sta = ACPI_STA_DEFAULT;
  1441. break;
  1442. case ACPI_TYPE_PROCESSOR:
  1443. *type = ACPI_BUS_TYPE_PROCESSOR;
  1444. status = acpi_bus_get_status_handle(handle, sta);
  1445. if (ACPI_FAILURE(status))
  1446. return -ENODEV;
  1447. break;
  1448. case ACPI_TYPE_THERMAL:
  1449. *type = ACPI_BUS_TYPE_THERMAL;
  1450. *sta = ACPI_STA_DEFAULT;
  1451. break;
  1452. case ACPI_TYPE_POWER:
  1453. *type = ACPI_BUS_TYPE_POWER;
  1454. *sta = ACPI_STA_DEFAULT;
  1455. break;
  1456. default:
  1457. return -ENODEV;
  1458. }
  1459. return 0;
  1460. }
  1461. bool acpi_device_is_present(const struct acpi_device *adev)
  1462. {
  1463. return adev->status.present || adev->status.functional;
  1464. }
  1465. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1466. const char *idstr,
  1467. const struct acpi_device_id **matchid)
  1468. {
  1469. const struct acpi_device_id *devid;
  1470. if (handler->match)
  1471. return handler->match(idstr, matchid);
  1472. for (devid = handler->ids; devid->id[0]; devid++)
  1473. if (!strcmp((char *)devid->id, idstr)) {
  1474. if (matchid)
  1475. *matchid = devid;
  1476. return true;
  1477. }
  1478. return false;
  1479. }
  1480. static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
  1481. const struct acpi_device_id **matchid)
  1482. {
  1483. struct acpi_scan_handler *handler;
  1484. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1485. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1486. return handler;
  1487. return NULL;
  1488. }
  1489. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1490. {
  1491. if (!!hotplug->enabled == !!val)
  1492. return;
  1493. mutex_lock(&acpi_scan_lock);
  1494. hotplug->enabled = val;
  1495. mutex_unlock(&acpi_scan_lock);
  1496. }
  1497. static void acpi_scan_init_hotplug(struct acpi_device *adev)
  1498. {
  1499. struct acpi_hardware_id *hwid;
  1500. if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
  1501. acpi_dock_add(adev);
  1502. return;
  1503. }
  1504. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  1505. struct acpi_scan_handler *handler;
  1506. handler = acpi_scan_match_handler(hwid->id, NULL);
  1507. if (handler) {
  1508. adev->flags.hotplug_notify = true;
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. static void acpi_device_dep_initialize(struct acpi_device *adev)
  1514. {
  1515. struct acpi_dep_data *dep;
  1516. struct acpi_handle_list dep_devices;
  1517. acpi_status status;
  1518. int i;
  1519. adev->dep_unmet = 0;
  1520. if (!acpi_has_method(adev->handle, "_DEP"))
  1521. return;
  1522. status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
  1523. &dep_devices);
  1524. if (ACPI_FAILURE(status)) {
  1525. dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
  1526. return;
  1527. }
  1528. for (i = 0; i < dep_devices.count; i++) {
  1529. struct acpi_device_info *info;
  1530. int skip;
  1531. status = acpi_get_object_info(dep_devices.handles[i], &info);
  1532. if (ACPI_FAILURE(status)) {
  1533. dev_dbg(&adev->dev, "Error reading _DEP device info\n");
  1534. continue;
  1535. }
  1536. /*
  1537. * Skip the dependency of Windows System Power
  1538. * Management Controller
  1539. */
  1540. skip = info->valid & ACPI_VALID_HID &&
  1541. !strcmp(info->hardware_id.string, "INT3396");
  1542. kfree(info);
  1543. if (skip)
  1544. continue;
  1545. dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
  1546. if (!dep)
  1547. return;
  1548. dep->master = dep_devices.handles[i];
  1549. dep->slave = adev->handle;
  1550. adev->dep_unmet++;
  1551. mutex_lock(&acpi_dep_list_lock);
  1552. list_add_tail(&dep->node , &acpi_dep_list);
  1553. mutex_unlock(&acpi_dep_list_lock);
  1554. }
  1555. }
  1556. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1557. void *not_used, void **return_value)
  1558. {
  1559. struct acpi_device *device = NULL;
  1560. int type;
  1561. unsigned long long sta;
  1562. int result;
  1563. acpi_bus_get_device(handle, &device);
  1564. if (device)
  1565. goto out;
  1566. result = acpi_bus_type_and_status(handle, &type, &sta);
  1567. if (result)
  1568. return AE_OK;
  1569. if (type == ACPI_BUS_TYPE_POWER) {
  1570. acpi_add_power_resource(handle);
  1571. return AE_OK;
  1572. }
  1573. acpi_add_single_object(&device, handle, type, sta);
  1574. if (!device)
  1575. return AE_CTRL_DEPTH;
  1576. acpi_scan_init_hotplug(device);
  1577. acpi_device_dep_initialize(device);
  1578. out:
  1579. if (!*return_value)
  1580. *return_value = device;
  1581. return AE_OK;
  1582. }
  1583. static void acpi_default_enumeration(struct acpi_device *device)
  1584. {
  1585. /*
  1586. * Do not enumerate devices with enumeration_by_parent flag set as
  1587. * they will be enumerated by their respective parents.
  1588. */
  1589. if (!device->flags.enumeration_by_parent) {
  1590. acpi_create_platform_device(device, NULL);
  1591. acpi_device_set_enumerated(device);
  1592. } else {
  1593. blocking_notifier_call_chain(&acpi_reconfig_chain,
  1594. ACPI_RECONFIG_DEVICE_ADD, device);
  1595. }
  1596. }
  1597. static const struct acpi_device_id generic_device_ids[] = {
  1598. {ACPI_DT_NAMESPACE_HID, },
  1599. {"", },
  1600. };
  1601. static int acpi_generic_device_attach(struct acpi_device *adev,
  1602. const struct acpi_device_id *not_used)
  1603. {
  1604. /*
  1605. * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
  1606. * below can be unconditional.
  1607. */
  1608. if (adev->data.of_compatible)
  1609. acpi_default_enumeration(adev);
  1610. return 1;
  1611. }
  1612. static struct acpi_scan_handler generic_device_handler = {
  1613. .ids = generic_device_ids,
  1614. .attach = acpi_generic_device_attach,
  1615. };
  1616. static int acpi_scan_attach_handler(struct acpi_device *device)
  1617. {
  1618. struct acpi_hardware_id *hwid;
  1619. int ret = 0;
  1620. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1621. const struct acpi_device_id *devid;
  1622. struct acpi_scan_handler *handler;
  1623. handler = acpi_scan_match_handler(hwid->id, &devid);
  1624. if (handler) {
  1625. if (!handler->attach) {
  1626. device->pnp.type.platform_id = 0;
  1627. continue;
  1628. }
  1629. device->handler = handler;
  1630. ret = handler->attach(device, devid);
  1631. if (ret > 0)
  1632. break;
  1633. device->handler = NULL;
  1634. if (ret < 0)
  1635. break;
  1636. }
  1637. }
  1638. return ret;
  1639. }
  1640. static void acpi_bus_attach(struct acpi_device *device)
  1641. {
  1642. struct acpi_device *child;
  1643. acpi_handle ejd;
  1644. int ret;
  1645. if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
  1646. register_dock_dependent_device(device, ejd);
  1647. acpi_bus_get_status(device);
  1648. /* Skip devices that are not present. */
  1649. if (!acpi_device_is_present(device)) {
  1650. device->flags.initialized = false;
  1651. acpi_device_clear_enumerated(device);
  1652. device->flags.power_manageable = 0;
  1653. return;
  1654. }
  1655. if (device->handler)
  1656. goto ok;
  1657. if (!device->flags.initialized) {
  1658. device->flags.power_manageable =
  1659. device->power.states[ACPI_STATE_D0].flags.valid;
  1660. if (acpi_bus_init_power(device))
  1661. device->flags.power_manageable = 0;
  1662. device->flags.initialized = true;
  1663. } else if (device->flags.visited) {
  1664. goto ok;
  1665. }
  1666. ret = acpi_scan_attach_handler(device);
  1667. if (ret < 0)
  1668. return;
  1669. device->flags.match_driver = true;
  1670. if (ret > 0 && !device->flags.enumeration_by_parent) {
  1671. acpi_device_set_enumerated(device);
  1672. goto ok;
  1673. }
  1674. ret = device_attach(&device->dev);
  1675. if (ret < 0)
  1676. return;
  1677. if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
  1678. acpi_default_enumeration(device);
  1679. else
  1680. acpi_device_set_enumerated(device);
  1681. ok:
  1682. list_for_each_entry(child, &device->children, node)
  1683. acpi_bus_attach(child);
  1684. if (device->handler && device->handler->hotplug.notify_online)
  1685. device->handler->hotplug.notify_online(device);
  1686. }
  1687. void acpi_walk_dep_device_list(acpi_handle handle)
  1688. {
  1689. struct acpi_dep_data *dep, *tmp;
  1690. struct acpi_device *adev;
  1691. mutex_lock(&acpi_dep_list_lock);
  1692. list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
  1693. if (dep->master == handle) {
  1694. acpi_bus_get_device(dep->slave, &adev);
  1695. if (!adev)
  1696. continue;
  1697. adev->dep_unmet--;
  1698. if (!adev->dep_unmet)
  1699. acpi_bus_attach(adev);
  1700. list_del(&dep->node);
  1701. kfree(dep);
  1702. }
  1703. }
  1704. mutex_unlock(&acpi_dep_list_lock);
  1705. }
  1706. EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
  1707. /**
  1708. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1709. * @handle: Root of the namespace scope to scan.
  1710. *
  1711. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1712. * found devices.
  1713. *
  1714. * If no devices were found, -ENODEV is returned, but it does not mean that
  1715. * there has been a real error. There just have been no suitable ACPI objects
  1716. * in the table trunk from which the kernel could create a device and add an
  1717. * appropriate driver.
  1718. *
  1719. * Must be called under acpi_scan_lock.
  1720. */
  1721. int acpi_bus_scan(acpi_handle handle)
  1722. {
  1723. void *device = NULL;
  1724. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1725. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1726. acpi_bus_check_add, NULL, NULL, &device);
  1727. if (device) {
  1728. acpi_bus_attach(device);
  1729. return 0;
  1730. }
  1731. return -ENODEV;
  1732. }
  1733. EXPORT_SYMBOL(acpi_bus_scan);
  1734. /**
  1735. * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
  1736. * @adev: Root of the ACPI namespace scope to walk.
  1737. *
  1738. * Must be called under acpi_scan_lock.
  1739. */
  1740. void acpi_bus_trim(struct acpi_device *adev)
  1741. {
  1742. struct acpi_scan_handler *handler = adev->handler;
  1743. struct acpi_device *child;
  1744. list_for_each_entry_reverse(child, &adev->children, node)
  1745. acpi_bus_trim(child);
  1746. adev->flags.match_driver = false;
  1747. if (handler) {
  1748. if (handler->detach)
  1749. handler->detach(adev);
  1750. adev->handler = NULL;
  1751. } else {
  1752. device_release_driver(&adev->dev);
  1753. }
  1754. /*
  1755. * Most likely, the device is going away, so put it into D3cold before
  1756. * that.
  1757. */
  1758. acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
  1759. adev->flags.initialized = false;
  1760. acpi_device_clear_enumerated(adev);
  1761. }
  1762. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1763. int acpi_bus_register_early_device(int type)
  1764. {
  1765. struct acpi_device *device = NULL;
  1766. int result;
  1767. result = acpi_add_single_object(&device, NULL,
  1768. type, ACPI_STA_DEFAULT);
  1769. if (result)
  1770. return result;
  1771. device->flags.match_driver = true;
  1772. return device_attach(&device->dev);
  1773. }
  1774. EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
  1775. static int acpi_bus_scan_fixed(void)
  1776. {
  1777. int result = 0;
  1778. /*
  1779. * Enumerate all fixed-feature devices.
  1780. */
  1781. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1782. struct acpi_device *device = NULL;
  1783. result = acpi_add_single_object(&device, NULL,
  1784. ACPI_BUS_TYPE_POWER_BUTTON,
  1785. ACPI_STA_DEFAULT);
  1786. if (result)
  1787. return result;
  1788. device->flags.match_driver = true;
  1789. result = device_attach(&device->dev);
  1790. if (result < 0)
  1791. return result;
  1792. device_init_wakeup(&device->dev, true);
  1793. }
  1794. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1795. struct acpi_device *device = NULL;
  1796. result = acpi_add_single_object(&device, NULL,
  1797. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1798. ACPI_STA_DEFAULT);
  1799. if (result)
  1800. return result;
  1801. device->flags.match_driver = true;
  1802. result = device_attach(&device->dev);
  1803. }
  1804. return result < 0 ? result : 0;
  1805. }
  1806. static void __init acpi_get_spcr_uart_addr(void)
  1807. {
  1808. acpi_status status;
  1809. struct acpi_table_spcr *spcr_ptr;
  1810. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  1811. (struct acpi_table_header **)&spcr_ptr);
  1812. if (ACPI_SUCCESS(status))
  1813. spcr_uart_addr = spcr_ptr->serial_port.address;
  1814. else
  1815. printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
  1816. }
  1817. static bool acpi_scan_initialized;
  1818. int __init acpi_scan_init(void)
  1819. {
  1820. int result;
  1821. acpi_status status;
  1822. struct acpi_table_stao *stao_ptr;
  1823. acpi_pci_root_init();
  1824. acpi_pci_link_init();
  1825. acpi_processor_init();
  1826. acpi_lpss_init();
  1827. acpi_apd_init();
  1828. acpi_cmos_rtc_init();
  1829. acpi_container_init();
  1830. acpi_memory_hotplug_init();
  1831. acpi_watchdog_init();
  1832. acpi_pnp_init();
  1833. acpi_int340x_thermal_init();
  1834. acpi_amba_init();
  1835. acpi_init_lpit();
  1836. acpi_scan_add_handler(&generic_device_handler);
  1837. /*
  1838. * If there is STAO table, check whether it needs to ignore the UART
  1839. * device in SPCR table.
  1840. */
  1841. status = acpi_get_table(ACPI_SIG_STAO, 0,
  1842. (struct acpi_table_header **)&stao_ptr);
  1843. if (ACPI_SUCCESS(status)) {
  1844. if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
  1845. printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
  1846. if (stao_ptr->ignore_uart)
  1847. acpi_get_spcr_uart_addr();
  1848. }
  1849. acpi_gpe_apply_masked_gpes();
  1850. acpi_update_all_gpes();
  1851. mutex_lock(&acpi_scan_lock);
  1852. /*
  1853. * Enumerate devices in the ACPI namespace.
  1854. */
  1855. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1856. if (result)
  1857. goto out;
  1858. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1859. if (result)
  1860. goto out;
  1861. /* Fixed feature devices do not exist on HW-reduced platform */
  1862. if (!acpi_gbl_reduced_hardware) {
  1863. result = acpi_bus_scan_fixed();
  1864. if (result) {
  1865. acpi_detach_data(acpi_root->handle,
  1866. acpi_scan_drop_device);
  1867. acpi_device_del(acpi_root);
  1868. put_device(&acpi_root->dev);
  1869. goto out;
  1870. }
  1871. }
  1872. acpi_scan_initialized = true;
  1873. out:
  1874. mutex_unlock(&acpi_scan_lock);
  1875. return result;
  1876. }
  1877. static struct acpi_probe_entry *ape;
  1878. static int acpi_probe_count;
  1879. static DEFINE_MUTEX(acpi_probe_mutex);
  1880. static int __init acpi_match_madt(struct acpi_subtable_header *header,
  1881. const unsigned long end)
  1882. {
  1883. if (!ape->subtable_valid || ape->subtable_valid(header, ape))
  1884. if (!ape->probe_subtbl(header, end))
  1885. acpi_probe_count++;
  1886. return 0;
  1887. }
  1888. int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
  1889. {
  1890. int count = 0;
  1891. if (acpi_disabled)
  1892. return 0;
  1893. mutex_lock(&acpi_probe_mutex);
  1894. for (ape = ap_head; nr; ape++, nr--) {
  1895. if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
  1896. acpi_probe_count = 0;
  1897. acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
  1898. count += acpi_probe_count;
  1899. } else {
  1900. int res;
  1901. res = acpi_table_parse(ape->id, ape->probe_table);
  1902. if (!res)
  1903. count++;
  1904. }
  1905. }
  1906. mutex_unlock(&acpi_probe_mutex);
  1907. return count;
  1908. }
  1909. struct acpi_table_events_work {
  1910. struct work_struct work;
  1911. void *table;
  1912. u32 event;
  1913. };
  1914. static void acpi_table_events_fn(struct work_struct *work)
  1915. {
  1916. struct acpi_table_events_work *tew;
  1917. tew = container_of(work, struct acpi_table_events_work, work);
  1918. if (tew->event == ACPI_TABLE_EVENT_LOAD) {
  1919. acpi_scan_lock_acquire();
  1920. acpi_bus_scan(ACPI_ROOT_OBJECT);
  1921. acpi_scan_lock_release();
  1922. }
  1923. kfree(tew);
  1924. }
  1925. void acpi_scan_table_handler(u32 event, void *table, void *context)
  1926. {
  1927. struct acpi_table_events_work *tew;
  1928. if (!acpi_scan_initialized)
  1929. return;
  1930. if (event != ACPI_TABLE_EVENT_LOAD)
  1931. return;
  1932. tew = kmalloc(sizeof(*tew), GFP_KERNEL);
  1933. if (!tew)
  1934. return;
  1935. INIT_WORK(&tew->work, acpi_table_events_fn);
  1936. tew->table = table;
  1937. tew->event = event;
  1938. schedule_work(&tew->work);
  1939. }
  1940. int acpi_reconfig_notifier_register(struct notifier_block *nb)
  1941. {
  1942. return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
  1943. }
  1944. EXPORT_SYMBOL(acpi_reconfig_notifier_register);
  1945. int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
  1946. {
  1947. return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
  1948. }
  1949. EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);