scan.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  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. ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
  413. if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
  414. list_del(&acpi_device_bus_id->node);
  415. kfree_const(acpi_device_bus_id->bus_id);
  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. *device = NULL;
  497. status = acpi_get_data_full(handle, acpi_scan_drop_device,
  498. (void **)device, callback);
  499. if (ACPI_FAILURE(status) || !*device) {
  500. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  501. handle));
  502. return -ENODEV;
  503. }
  504. return 0;
  505. }
  506. int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
  507. {
  508. return acpi_get_device_data(handle, device, NULL);
  509. }
  510. EXPORT_SYMBOL(acpi_bus_get_device);
  511. static void get_acpi_device(void *dev)
  512. {
  513. if (dev)
  514. get_device(&((struct acpi_device *)dev)->dev);
  515. }
  516. struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
  517. {
  518. struct acpi_device *adev = NULL;
  519. acpi_get_device_data(handle, &adev, get_acpi_device);
  520. return adev;
  521. }
  522. void acpi_bus_put_acpi_device(struct acpi_device *adev)
  523. {
  524. put_device(&adev->dev);
  525. }
  526. static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
  527. {
  528. struct acpi_device_bus_id *acpi_device_bus_id;
  529. /* Find suitable bus_id and instance number in acpi_bus_id_list. */
  530. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  531. if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
  532. return acpi_device_bus_id;
  533. }
  534. return NULL;
  535. }
  536. static int acpi_device_set_name(struct acpi_device *device,
  537. struct acpi_device_bus_id *acpi_device_bus_id)
  538. {
  539. struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
  540. int result;
  541. result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
  542. if (result < 0)
  543. return result;
  544. device->pnp.instance_no = result;
  545. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
  546. return 0;
  547. }
  548. int acpi_device_add(struct acpi_device *device,
  549. void (*release)(struct device *))
  550. {
  551. struct acpi_device_bus_id *acpi_device_bus_id;
  552. int result;
  553. if (device->handle) {
  554. acpi_status status;
  555. status = acpi_attach_data(device->handle, acpi_scan_drop_device,
  556. device);
  557. if (ACPI_FAILURE(status)) {
  558. acpi_handle_err(device->handle,
  559. "Unable to attach device data\n");
  560. return -ENODEV;
  561. }
  562. }
  563. /*
  564. * Linkage
  565. * -------
  566. * Link this device to its parent and siblings.
  567. */
  568. INIT_LIST_HEAD(&device->children);
  569. INIT_LIST_HEAD(&device->node);
  570. INIT_LIST_HEAD(&device->wakeup_list);
  571. INIT_LIST_HEAD(&device->physical_node_list);
  572. INIT_LIST_HEAD(&device->del_list);
  573. mutex_init(&device->physical_node_lock);
  574. mutex_lock(&acpi_device_lock);
  575. acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
  576. if (acpi_device_bus_id) {
  577. result = acpi_device_set_name(device, acpi_device_bus_id);
  578. if (result)
  579. goto err_unlock;
  580. } else {
  581. acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
  582. GFP_KERNEL);
  583. if (!acpi_device_bus_id) {
  584. result = -ENOMEM;
  585. goto err_unlock;
  586. }
  587. acpi_device_bus_id->bus_id =
  588. kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
  589. if (!acpi_device_bus_id->bus_id) {
  590. kfree(acpi_device_bus_id);
  591. result = -ENOMEM;
  592. goto err_unlock;
  593. }
  594. ida_init(&acpi_device_bus_id->instance_ida);
  595. result = acpi_device_set_name(device, acpi_device_bus_id);
  596. if (result) {
  597. kfree_const(acpi_device_bus_id->bus_id);
  598. kfree(acpi_device_bus_id);
  599. goto err_unlock;
  600. }
  601. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  602. }
  603. if (device->parent)
  604. list_add_tail(&device->node, &device->parent->children);
  605. if (device->wakeup.flags.valid)
  606. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  607. mutex_unlock(&acpi_device_lock);
  608. if (device->parent)
  609. device->dev.parent = &device->parent->dev;
  610. device->dev.bus = &acpi_bus_type;
  611. device->dev.release = release;
  612. result = device_add(&device->dev);
  613. if (result) {
  614. dev_err(&device->dev, "Error registering device\n");
  615. goto err;
  616. }
  617. result = acpi_device_setup_files(device);
  618. if (result)
  619. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  620. dev_name(&device->dev));
  621. return 0;
  622. err:
  623. mutex_lock(&acpi_device_lock);
  624. if (device->parent)
  625. list_del(&device->node);
  626. list_del(&device->wakeup_list);
  627. err_unlock:
  628. mutex_unlock(&acpi_device_lock);
  629. acpi_detach_data(device->handle, acpi_scan_drop_device);
  630. return result;
  631. }
  632. /* --------------------------------------------------------------------------
  633. Device Enumeration
  634. -------------------------------------------------------------------------- */
  635. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  636. {
  637. struct acpi_device *device = NULL;
  638. acpi_status status;
  639. /*
  640. * Fixed hardware devices do not appear in the namespace and do not
  641. * have handles, but we fabricate acpi_devices for them, so we have
  642. * to deal with them specially.
  643. */
  644. if (!handle)
  645. return acpi_root;
  646. do {
  647. status = acpi_get_parent(handle, &handle);
  648. if (ACPI_FAILURE(status))
  649. return status == AE_NULL_ENTRY ? NULL : acpi_root;
  650. } while (acpi_bus_get_device(handle, &device));
  651. return device;
  652. }
  653. acpi_status
  654. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  655. {
  656. acpi_status status;
  657. acpi_handle tmp;
  658. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  659. union acpi_object *obj;
  660. status = acpi_get_handle(handle, "_EJD", &tmp);
  661. if (ACPI_FAILURE(status))
  662. return status;
  663. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  664. if (ACPI_SUCCESS(status)) {
  665. obj = buffer.pointer;
  666. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  667. ejd);
  668. kfree(buffer.pointer);
  669. }
  670. return status;
  671. }
  672. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  673. static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  674. struct acpi_device_wakeup *wakeup)
  675. {
  676. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  677. union acpi_object *package = NULL;
  678. union acpi_object *element = NULL;
  679. acpi_status status;
  680. int err = -ENODATA;
  681. if (!wakeup)
  682. return -EINVAL;
  683. INIT_LIST_HEAD(&wakeup->resources);
  684. /* _PRW */
  685. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  686. if (ACPI_FAILURE(status)) {
  687. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  688. return err;
  689. }
  690. package = (union acpi_object *)buffer.pointer;
  691. if (!package || package->package.count < 2)
  692. goto out;
  693. element = &(package->package.elements[0]);
  694. if (!element)
  695. goto out;
  696. if (element->type == ACPI_TYPE_PACKAGE) {
  697. if ((element->package.count < 2) ||
  698. (element->package.elements[0].type !=
  699. ACPI_TYPE_LOCAL_REFERENCE)
  700. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  701. goto out;
  702. wakeup->gpe_device =
  703. element->package.elements[0].reference.handle;
  704. wakeup->gpe_number =
  705. (u32) element->package.elements[1].integer.value;
  706. } else if (element->type == ACPI_TYPE_INTEGER) {
  707. wakeup->gpe_device = NULL;
  708. wakeup->gpe_number = element->integer.value;
  709. } else {
  710. goto out;
  711. }
  712. element = &(package->package.elements[1]);
  713. if (element->type != ACPI_TYPE_INTEGER)
  714. goto out;
  715. wakeup->sleep_state = element->integer.value;
  716. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  717. if (err)
  718. goto out;
  719. if (!list_empty(&wakeup->resources)) {
  720. int sleep_state;
  721. err = acpi_power_wakeup_list_init(&wakeup->resources,
  722. &sleep_state);
  723. if (err) {
  724. acpi_handle_warn(handle, "Retrieving current states "
  725. "of wakeup power resources failed\n");
  726. acpi_power_resources_list_free(&wakeup->resources);
  727. goto out;
  728. }
  729. if (sleep_state < wakeup->sleep_state) {
  730. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  731. "(S%d) by S%d from power resources\n",
  732. (int)wakeup->sleep_state, sleep_state);
  733. wakeup->sleep_state = sleep_state;
  734. }
  735. }
  736. out:
  737. kfree(buffer.pointer);
  738. return err;
  739. }
  740. static bool acpi_wakeup_gpe_init(struct acpi_device *device)
  741. {
  742. static const struct acpi_device_id button_device_ids[] = {
  743. {"PNP0C0C", 0},
  744. {"PNP0C0D", 0},
  745. {"PNP0C0E", 0},
  746. {"", 0},
  747. };
  748. struct acpi_device_wakeup *wakeup = &device->wakeup;
  749. acpi_status status;
  750. wakeup->flags.notifier_present = 0;
  751. /* Power button, Lid switch always enable wakeup */
  752. if (!acpi_match_device_ids(device, button_device_ids)) {
  753. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  754. /* Do not use Lid/sleep button for S5 wakeup */
  755. if (wakeup->sleep_state == ACPI_STATE_S5)
  756. wakeup->sleep_state = ACPI_STATE_S4;
  757. }
  758. acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
  759. device_set_wakeup_capable(&device->dev, true);
  760. return true;
  761. }
  762. status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
  763. wakeup->gpe_number);
  764. return ACPI_SUCCESS(status);
  765. }
  766. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  767. {
  768. int err;
  769. /* Presence of _PRW indicates wake capable */
  770. if (!acpi_has_method(device->handle, "_PRW"))
  771. return;
  772. err = acpi_bus_extract_wakeup_device_power_package(device->handle,
  773. &device->wakeup);
  774. if (err) {
  775. dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
  776. return;
  777. }
  778. device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
  779. device->wakeup.prepare_count = 0;
  780. /*
  781. * Call _PSW/_DSW object to disable its ability to wake the sleeping
  782. * system for the ACPI device with the _PRW object.
  783. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  784. * So it is necessary to call _DSW object first. Only when it is not
  785. * present will the _PSW object used.
  786. */
  787. err = acpi_device_sleep_wake(device, 0, 0, 0);
  788. if (err)
  789. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  790. "error in _DSW or _PSW evaluation\n"));
  791. }
  792. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  793. {
  794. struct acpi_device_power_state *ps = &device->power.states[state];
  795. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  796. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  797. acpi_status status;
  798. INIT_LIST_HEAD(&ps->resources);
  799. /* Evaluate "_PRx" to get referenced power resources */
  800. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  801. if (ACPI_SUCCESS(status)) {
  802. union acpi_object *package = buffer.pointer;
  803. if (buffer.length && package
  804. && package->type == ACPI_TYPE_PACKAGE
  805. && package->package.count)
  806. acpi_extract_power_resources(package, 0, &ps->resources);
  807. ACPI_FREE(buffer.pointer);
  808. }
  809. /* Evaluate "_PSx" to see if we can do explicit sets */
  810. pathname[2] = 'S';
  811. if (acpi_has_method(device->handle, pathname))
  812. ps->flags.explicit_set = 1;
  813. /* State is valid if there are means to put the device into it. */
  814. if (!list_empty(&ps->resources) || ps->flags.explicit_set)
  815. ps->flags.valid = 1;
  816. ps->power = -1; /* Unknown - driver assigned */
  817. ps->latency = -1; /* Unknown - driver assigned */
  818. }
  819. static void acpi_bus_get_power_flags(struct acpi_device *device)
  820. {
  821. u32 i;
  822. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  823. if (!acpi_has_method(device->handle, "_PS0") &&
  824. !acpi_has_method(device->handle, "_PR0"))
  825. return;
  826. device->flags.power_manageable = 1;
  827. /*
  828. * Power Management Flags
  829. */
  830. if (acpi_has_method(device->handle, "_PSC"))
  831. device->power.flags.explicit_get = 1;
  832. if (acpi_has_method(device->handle, "_IRC"))
  833. device->power.flags.inrush_current = 1;
  834. if (acpi_has_method(device->handle, "_DSW"))
  835. device->power.flags.dsw_present = 1;
  836. /*
  837. * Enumerate supported power management states
  838. */
  839. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  840. acpi_bus_init_power_state(device, i);
  841. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  842. /* Set the defaults for D0 and D3hot (always supported). */
  843. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  844. device->power.states[ACPI_STATE_D0].power = 100;
  845. device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
  846. /*
  847. * Use power resources only if the D0 list of them is populated, because
  848. * some platforms may provide _PR3 only to indicate D3cold support and
  849. * in those cases the power resources list returned by it may be bogus.
  850. */
  851. if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
  852. device->power.flags.power_resources = 1;
  853. /*
  854. * D3cold is supported if the D3hot list of power resources is
  855. * not empty.
  856. */
  857. if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
  858. device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
  859. }
  860. if (acpi_bus_init_power(device))
  861. device->flags.power_manageable = 0;
  862. }
  863. static void acpi_bus_get_flags(struct acpi_device *device)
  864. {
  865. /* Presence of _STA indicates 'dynamic_status' */
  866. if (acpi_has_method(device->handle, "_STA"))
  867. device->flags.dynamic_status = 1;
  868. /* Presence of _RMV indicates 'removable' */
  869. if (acpi_has_method(device->handle, "_RMV"))
  870. device->flags.removable = 1;
  871. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  872. if (acpi_has_method(device->handle, "_EJD") ||
  873. acpi_has_method(device->handle, "_EJ0"))
  874. device->flags.ejectable = 1;
  875. }
  876. static void acpi_device_get_busid(struct acpi_device *device)
  877. {
  878. char bus_id[5] = { '?', 0 };
  879. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  880. int i = 0;
  881. /*
  882. * Bus ID
  883. * ------
  884. * The device's Bus ID is simply the object name.
  885. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  886. */
  887. if (ACPI_IS_ROOT_DEVICE(device)) {
  888. strcpy(device->pnp.bus_id, "ACPI");
  889. return;
  890. }
  891. switch (device->device_type) {
  892. case ACPI_BUS_TYPE_POWER_BUTTON:
  893. strcpy(device->pnp.bus_id, "PWRF");
  894. break;
  895. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  896. strcpy(device->pnp.bus_id, "SLPF");
  897. break;
  898. case ACPI_BUS_TYPE_ECDT_EC:
  899. strcpy(device->pnp.bus_id, "ECDT");
  900. break;
  901. default:
  902. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  903. /* Clean up trailing underscores (if any) */
  904. for (i = 3; i > 1; i--) {
  905. if (bus_id[i] == '_')
  906. bus_id[i] = '\0';
  907. else
  908. break;
  909. }
  910. strcpy(device->pnp.bus_id, bus_id);
  911. break;
  912. }
  913. }
  914. /*
  915. * acpi_ata_match - see if an acpi object is an ATA device
  916. *
  917. * If an acpi object has one of the ACPI ATA methods defined,
  918. * then we can safely call it an ATA device.
  919. */
  920. bool acpi_ata_match(acpi_handle handle)
  921. {
  922. return acpi_has_method(handle, "_GTF") ||
  923. acpi_has_method(handle, "_GTM") ||
  924. acpi_has_method(handle, "_STM") ||
  925. acpi_has_method(handle, "_SDD");
  926. }
  927. /*
  928. * acpi_bay_match - see if an acpi object is an ejectable driver bay
  929. *
  930. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  931. * then we can safely call it an ejectable drive bay
  932. */
  933. bool acpi_bay_match(acpi_handle handle)
  934. {
  935. acpi_handle phandle;
  936. if (!acpi_has_method(handle, "_EJ0"))
  937. return false;
  938. if (acpi_ata_match(handle))
  939. return true;
  940. if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
  941. return false;
  942. return acpi_ata_match(phandle);
  943. }
  944. bool acpi_device_is_battery(struct acpi_device *adev)
  945. {
  946. struct acpi_hardware_id *hwid;
  947. list_for_each_entry(hwid, &adev->pnp.ids, list)
  948. if (!strcmp("PNP0C0A", hwid->id))
  949. return true;
  950. return false;
  951. }
  952. static bool is_ejectable_bay(struct acpi_device *adev)
  953. {
  954. acpi_handle handle = adev->handle;
  955. if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
  956. return true;
  957. return acpi_bay_match(handle);
  958. }
  959. /*
  960. * acpi_dock_match - see if an acpi object has a _DCK method
  961. */
  962. bool acpi_dock_match(acpi_handle handle)
  963. {
  964. return acpi_has_method(handle, "_DCK");
  965. }
  966. static acpi_status
  967. acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
  968. void **return_value)
  969. {
  970. long *cap = context;
  971. if (acpi_has_method(handle, "_BCM") &&
  972. acpi_has_method(handle, "_BCL")) {
  973. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
  974. "support\n"));
  975. *cap |= ACPI_VIDEO_BACKLIGHT;
  976. /* We have backlight support, no need to scan further */
  977. return AE_CTRL_TERMINATE;
  978. }
  979. return 0;
  980. }
  981. /* Returns true if the ACPI object is a video device which can be
  982. * handled by video.ko.
  983. * The device will get a Linux specific CID added in scan.c to
  984. * identify the device as an ACPI graphics device
  985. * Be aware that the graphics device may not be physically present
  986. * Use acpi_video_get_capabilities() to detect general ACPI video
  987. * capabilities of present cards
  988. */
  989. long acpi_is_video_device(acpi_handle handle)
  990. {
  991. long video_caps = 0;
  992. /* Is this device able to support video switching ? */
  993. if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
  994. video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
  995. /* Is this device able to retrieve a video ROM ? */
  996. if (acpi_has_method(handle, "_ROM"))
  997. video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
  998. /* Is this device able to configure which video head to be POSTed ? */
  999. if (acpi_has_method(handle, "_VPO") &&
  1000. acpi_has_method(handle, "_GPD") &&
  1001. acpi_has_method(handle, "_SPD"))
  1002. video_caps |= ACPI_VIDEO_DEVICE_POSTING;
  1003. /* Only check for backlight functionality if one of the above hit. */
  1004. if (video_caps)
  1005. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1006. ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
  1007. &video_caps, NULL);
  1008. return video_caps;
  1009. }
  1010. EXPORT_SYMBOL(acpi_is_video_device);
  1011. const char *acpi_device_hid(struct acpi_device *device)
  1012. {
  1013. struct acpi_hardware_id *hid;
  1014. if (list_empty(&device->pnp.ids))
  1015. return dummy_hid;
  1016. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1017. return hid->id;
  1018. }
  1019. EXPORT_SYMBOL(acpi_device_hid);
  1020. static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
  1021. {
  1022. struct acpi_hardware_id *id;
  1023. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1024. if (!id)
  1025. return;
  1026. id->id = kstrdup_const(dev_id, GFP_KERNEL);
  1027. if (!id->id) {
  1028. kfree(id);
  1029. return;
  1030. }
  1031. list_add_tail(&id->list, &pnp->ids);
  1032. pnp->type.hardware_id = 1;
  1033. }
  1034. /*
  1035. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1036. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1037. * prefix. Work around this.
  1038. */
  1039. static bool acpi_ibm_smbus_match(acpi_handle handle)
  1040. {
  1041. char node_name[ACPI_PATH_SEGMENT_LENGTH];
  1042. struct acpi_buffer path = { sizeof(node_name), node_name };
  1043. if (!dmi_name_in_vendors("IBM"))
  1044. return false;
  1045. /* Look for SMBS object */
  1046. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
  1047. strcmp("SMBS", path.pointer))
  1048. return false;
  1049. /* Does it have the necessary (but misnamed) methods? */
  1050. if (acpi_has_method(handle, "SBI") &&
  1051. acpi_has_method(handle, "SBR") &&
  1052. acpi_has_method(handle, "SBW"))
  1053. return true;
  1054. return false;
  1055. }
  1056. static bool acpi_object_is_system_bus(acpi_handle handle)
  1057. {
  1058. acpi_handle tmp;
  1059. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
  1060. tmp == handle)
  1061. return true;
  1062. if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
  1063. tmp == handle)
  1064. return true;
  1065. return false;
  1066. }
  1067. static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
  1068. int device_type)
  1069. {
  1070. acpi_status status;
  1071. struct acpi_device_info *info;
  1072. struct acpi_pnp_device_id_list *cid_list;
  1073. int i;
  1074. switch (device_type) {
  1075. case ACPI_BUS_TYPE_DEVICE:
  1076. if (handle == ACPI_ROOT_OBJECT) {
  1077. acpi_add_id(pnp, ACPI_SYSTEM_HID);
  1078. break;
  1079. }
  1080. status = acpi_get_object_info(handle, &info);
  1081. if (ACPI_FAILURE(status)) {
  1082. pr_err(PREFIX "%s: Error reading device info\n",
  1083. __func__);
  1084. return;
  1085. }
  1086. if (info->valid & ACPI_VALID_HID) {
  1087. acpi_add_id(pnp, info->hardware_id.string);
  1088. pnp->type.platform_id = 1;
  1089. }
  1090. if (info->valid & ACPI_VALID_CID) {
  1091. cid_list = &info->compatible_id_list;
  1092. for (i = 0; i < cid_list->count; i++)
  1093. acpi_add_id(pnp, cid_list->ids[i].string);
  1094. }
  1095. if (info->valid & ACPI_VALID_ADR) {
  1096. pnp->bus_address = info->address;
  1097. pnp->type.bus_address = 1;
  1098. }
  1099. if (info->valid & ACPI_VALID_UID)
  1100. pnp->unique_id = kstrdup(info->unique_id.string,
  1101. GFP_KERNEL);
  1102. if (info->valid & ACPI_VALID_CLS)
  1103. acpi_add_id(pnp, info->class_code.string);
  1104. kfree(info);
  1105. /*
  1106. * Some devices don't reliably have _HIDs & _CIDs, so add
  1107. * synthetic HIDs to make sure drivers can find them.
  1108. */
  1109. if (acpi_is_video_device(handle))
  1110. acpi_add_id(pnp, ACPI_VIDEO_HID);
  1111. else if (acpi_bay_match(handle))
  1112. acpi_add_id(pnp, ACPI_BAY_HID);
  1113. else if (acpi_dock_match(handle))
  1114. acpi_add_id(pnp, ACPI_DOCK_HID);
  1115. else if (acpi_ibm_smbus_match(handle))
  1116. acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
  1117. else if (list_empty(&pnp->ids) &&
  1118. acpi_object_is_system_bus(handle)) {
  1119. /* \_SB, \_TZ, LNXSYBUS */
  1120. acpi_add_id(pnp, ACPI_BUS_HID);
  1121. strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
  1122. strcpy(pnp->device_class, ACPI_BUS_CLASS);
  1123. }
  1124. break;
  1125. case ACPI_BUS_TYPE_POWER:
  1126. acpi_add_id(pnp, ACPI_POWER_HID);
  1127. break;
  1128. case ACPI_BUS_TYPE_PROCESSOR:
  1129. acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
  1130. break;
  1131. case ACPI_BUS_TYPE_THERMAL:
  1132. acpi_add_id(pnp, ACPI_THERMAL_HID);
  1133. break;
  1134. case ACPI_BUS_TYPE_POWER_BUTTON:
  1135. acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
  1136. break;
  1137. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1138. acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
  1139. break;
  1140. case ACPI_BUS_TYPE_ECDT_EC:
  1141. acpi_add_id(pnp, ACPI_ECDT_HID);
  1142. break;
  1143. }
  1144. }
  1145. void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
  1146. {
  1147. struct acpi_hardware_id *id, *tmp;
  1148. list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
  1149. kfree_const(id->id);
  1150. kfree(id);
  1151. }
  1152. kfree(pnp->unique_id);
  1153. }
  1154. /**
  1155. * acpi_dma_supported - Check DMA support for the specified device.
  1156. * @adev: The pointer to acpi device
  1157. *
  1158. * Return false if DMA is not supported. Otherwise, return true
  1159. */
  1160. bool acpi_dma_supported(struct acpi_device *adev)
  1161. {
  1162. if (!adev)
  1163. return false;
  1164. if (adev->flags.cca_seen)
  1165. return true;
  1166. /*
  1167. * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
  1168. * DMA on "Intel platforms". Presumably that includes all x86 and
  1169. * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
  1170. */
  1171. if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1172. return true;
  1173. return false;
  1174. }
  1175. /**
  1176. * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
  1177. * @adev: The pointer to acpi device
  1178. *
  1179. * Return enum dev_dma_attr.
  1180. */
  1181. enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
  1182. {
  1183. if (!acpi_dma_supported(adev))
  1184. return DEV_DMA_NOT_SUPPORTED;
  1185. if (adev->flags.coherent_dma)
  1186. return DEV_DMA_COHERENT;
  1187. else
  1188. return DEV_DMA_NON_COHERENT;
  1189. }
  1190. /**
  1191. * acpi_dma_get_range() - Get device DMA parameters.
  1192. *
  1193. * @dev: device to configure
  1194. * @dma_addr: pointer device DMA address result
  1195. * @offset: pointer to the DMA offset result
  1196. * @size: pointer to DMA range size result
  1197. *
  1198. * Evaluate DMA regions and return respectively DMA region start, offset
  1199. * and size in dma_addr, offset and size on parsing success; it does not
  1200. * update the passed in values on failure.
  1201. *
  1202. * Return 0 on success, < 0 on failure.
  1203. */
  1204. int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
  1205. u64 *size)
  1206. {
  1207. struct acpi_device *adev;
  1208. LIST_HEAD(list);
  1209. struct resource_entry *rentry;
  1210. int ret;
  1211. struct device *dma_dev = dev;
  1212. u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
  1213. /*
  1214. * Walk the device tree chasing an ACPI companion with a _DMA
  1215. * object while we go. Stop if we find a device with an ACPI
  1216. * companion containing a _DMA method.
  1217. */
  1218. do {
  1219. adev = ACPI_COMPANION(dma_dev);
  1220. if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
  1221. break;
  1222. dma_dev = dma_dev->parent;
  1223. } while (dma_dev);
  1224. if (!dma_dev)
  1225. return -ENODEV;
  1226. if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
  1227. acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
  1228. return -EINVAL;
  1229. }
  1230. ret = acpi_dev_get_dma_resources(adev, &list);
  1231. if (ret > 0) {
  1232. list_for_each_entry(rentry, &list, node) {
  1233. if (dma_offset && rentry->offset != dma_offset) {
  1234. ret = -EINVAL;
  1235. dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
  1236. goto out;
  1237. }
  1238. dma_offset = rentry->offset;
  1239. /* Take lower and upper limits */
  1240. if (rentry->res->start < dma_start)
  1241. dma_start = rentry->res->start;
  1242. if (rentry->res->end > dma_end)
  1243. dma_end = rentry->res->end;
  1244. }
  1245. if (dma_start >= dma_end) {
  1246. ret = -EINVAL;
  1247. dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
  1248. goto out;
  1249. }
  1250. *dma_addr = dma_start - dma_offset;
  1251. len = dma_end - dma_start;
  1252. *size = max(len, len + 1);
  1253. *offset = dma_offset;
  1254. }
  1255. out:
  1256. acpi_dev_free_resource_list(&list);
  1257. return ret >= 0 ? 0 : ret;
  1258. }
  1259. /**
  1260. * acpi_dma_configure - Set-up DMA configuration for the device.
  1261. * @dev: The pointer to the device
  1262. * @attr: device dma attributes
  1263. */
  1264. int acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
  1265. {
  1266. const struct iommu_ops *iommu;
  1267. u64 dma_addr = 0, size = 0;
  1268. iort_dma_setup(dev, &dma_addr, &size);
  1269. iommu = iort_iommu_configure(dev);
  1270. if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
  1271. return -EPROBE_DEFER;
  1272. arch_setup_dma_ops(dev, dma_addr, size,
  1273. iommu, attr == DEV_DMA_COHERENT);
  1274. return 0;
  1275. }
  1276. EXPORT_SYMBOL_GPL(acpi_dma_configure);
  1277. /**
  1278. * acpi_dma_deconfigure - Tear-down DMA configuration for the device.
  1279. * @dev: The pointer to the device
  1280. */
  1281. void acpi_dma_deconfigure(struct device *dev)
  1282. {
  1283. arch_teardown_dma_ops(dev);
  1284. }
  1285. EXPORT_SYMBOL_GPL(acpi_dma_deconfigure);
  1286. static void acpi_init_coherency(struct acpi_device *adev)
  1287. {
  1288. unsigned long long cca = 0;
  1289. acpi_status status;
  1290. struct acpi_device *parent = adev->parent;
  1291. if (parent && parent->flags.cca_seen) {
  1292. /*
  1293. * From ACPI spec, OSPM will ignore _CCA if an ancestor
  1294. * already saw one.
  1295. */
  1296. adev->flags.cca_seen = 1;
  1297. cca = parent->flags.coherent_dma;
  1298. } else {
  1299. status = acpi_evaluate_integer(adev->handle, "_CCA",
  1300. NULL, &cca);
  1301. if (ACPI_SUCCESS(status))
  1302. adev->flags.cca_seen = 1;
  1303. else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
  1304. /*
  1305. * If architecture does not specify that _CCA is
  1306. * required for DMA-able devices (e.g. x86),
  1307. * we default to _CCA=1.
  1308. */
  1309. cca = 1;
  1310. else
  1311. acpi_handle_debug(adev->handle,
  1312. "ACPI device is missing _CCA.\n");
  1313. }
  1314. adev->flags.coherent_dma = cca;
  1315. }
  1316. static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
  1317. {
  1318. bool *is_serial_bus_slave_p = data;
  1319. if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  1320. return 1;
  1321. *is_serial_bus_slave_p = true;
  1322. /* no need to do more checking */
  1323. return -1;
  1324. }
  1325. static bool acpi_is_indirect_io_slave(struct acpi_device *device)
  1326. {
  1327. struct acpi_device *parent = device->parent;
  1328. static const struct acpi_device_id indirect_io_hosts[] = {
  1329. {"HISI0191", 0},
  1330. {}
  1331. };
  1332. return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
  1333. }
  1334. static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
  1335. {
  1336. struct list_head resource_list;
  1337. bool is_serial_bus_slave = false;
  1338. /*
  1339. * These devices have multiple I2cSerialBus resources and an i2c-client
  1340. * must be instantiated for each, each with its own i2c_device_id.
  1341. * Normally we only instantiate an i2c-client for the first resource,
  1342. * using the ACPI HID as id. These special cases are handled by the
  1343. * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
  1344. * which i2c_device_id to use for each resource.
  1345. */
  1346. static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
  1347. {"BSG1160", },
  1348. {"INT33FE", },
  1349. {}
  1350. };
  1351. if (acpi_is_indirect_io_slave(device))
  1352. return true;
  1353. /* Macs use device properties in lieu of _CRS resources */
  1354. if (x86_apple_machine &&
  1355. (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
  1356. fwnode_property_present(&device->fwnode, "i2cAddress") ||
  1357. fwnode_property_present(&device->fwnode, "baud")))
  1358. return true;
  1359. /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
  1360. if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
  1361. return false;
  1362. INIT_LIST_HEAD(&resource_list);
  1363. acpi_dev_get_resources(device, &resource_list,
  1364. acpi_check_serial_bus_slave,
  1365. &is_serial_bus_slave);
  1366. acpi_dev_free_resource_list(&resource_list);
  1367. return is_serial_bus_slave;
  1368. }
  1369. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1370. int type, unsigned long long sta)
  1371. {
  1372. INIT_LIST_HEAD(&device->pnp.ids);
  1373. device->device_type = type;
  1374. device->handle = handle;
  1375. device->parent = acpi_bus_get_parent(handle);
  1376. device->fwnode.ops = &acpi_device_fwnode_ops;
  1377. acpi_set_device_status(device, sta);
  1378. acpi_device_get_busid(device);
  1379. acpi_set_pnp_ids(handle, &device->pnp, type);
  1380. acpi_init_properties(device);
  1381. acpi_bus_get_flags(device);
  1382. device->flags.match_driver = false;
  1383. device->flags.initialized = true;
  1384. device->flags.enumeration_by_parent =
  1385. acpi_device_enumeration_by_parent(device);
  1386. acpi_device_clear_enumerated(device);
  1387. device_initialize(&device->dev);
  1388. dev_set_uevent_suppress(&device->dev, true);
  1389. acpi_init_coherency(device);
  1390. /* Assume there are unmet deps until acpi_device_dep_initialize() runs */
  1391. device->dep_unmet = 1;
  1392. }
  1393. void acpi_device_add_finalize(struct acpi_device *device)
  1394. {
  1395. dev_set_uevent_suppress(&device->dev, false);
  1396. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1397. }
  1398. static int acpi_add_single_object(struct acpi_device **child,
  1399. acpi_handle handle, int type,
  1400. unsigned long long sta)
  1401. {
  1402. int result;
  1403. struct acpi_device *device;
  1404. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1405. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1406. if (!device) {
  1407. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1408. return -ENOMEM;
  1409. }
  1410. acpi_init_device_object(device, handle, type, sta);
  1411. /*
  1412. * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
  1413. * that we can call acpi_bus_get_status() and use its quirk handling.
  1414. * Note this must be done before the get power-/wakeup_dev-flags calls.
  1415. */
  1416. if (type == ACPI_BUS_TYPE_DEVICE)
  1417. if (acpi_bus_get_status(device) < 0)
  1418. acpi_set_device_status(device, 0);
  1419. acpi_bus_get_power_flags(device);
  1420. acpi_bus_get_wakeup_device_flags(device);
  1421. result = acpi_device_add(device, acpi_device_release);
  1422. if (result) {
  1423. acpi_device_release(&device->dev);
  1424. return result;
  1425. }
  1426. acpi_power_add_remove_device(device, true);
  1427. acpi_device_add_finalize(device);
  1428. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1429. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1430. dev_name(&device->dev), (char *) buffer.pointer,
  1431. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1432. kfree(buffer.pointer);
  1433. *child = device;
  1434. return 0;
  1435. }
  1436. static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
  1437. void *context)
  1438. {
  1439. struct resource *res = context;
  1440. if (acpi_dev_resource_memory(ares, res))
  1441. return AE_CTRL_TERMINATE;
  1442. return AE_OK;
  1443. }
  1444. static bool acpi_device_should_be_hidden(acpi_handle handle)
  1445. {
  1446. acpi_status status;
  1447. struct resource res;
  1448. /* Check if it should ignore the UART device */
  1449. if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
  1450. return false;
  1451. /*
  1452. * The UART device described in SPCR table is assumed to have only one
  1453. * memory resource present. So we only look for the first one here.
  1454. */
  1455. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1456. acpi_get_resource_memory, &res);
  1457. if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
  1458. return false;
  1459. acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
  1460. &res.start);
  1461. return true;
  1462. }
  1463. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1464. unsigned long long *sta)
  1465. {
  1466. acpi_status status;
  1467. acpi_object_type acpi_type;
  1468. status = acpi_get_type(handle, &acpi_type);
  1469. if (ACPI_FAILURE(status))
  1470. return -ENODEV;
  1471. switch (acpi_type) {
  1472. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1473. case ACPI_TYPE_DEVICE:
  1474. if (acpi_device_should_be_hidden(handle))
  1475. return -ENODEV;
  1476. *type = ACPI_BUS_TYPE_DEVICE;
  1477. /*
  1478. * acpi_add_single_object updates this once we've an acpi_device
  1479. * so that acpi_bus_get_status' quirk handling can be used.
  1480. */
  1481. *sta = ACPI_STA_DEFAULT;
  1482. break;
  1483. case ACPI_TYPE_PROCESSOR:
  1484. *type = ACPI_BUS_TYPE_PROCESSOR;
  1485. status = acpi_bus_get_status_handle(handle, sta);
  1486. if (ACPI_FAILURE(status))
  1487. return -ENODEV;
  1488. break;
  1489. case ACPI_TYPE_THERMAL:
  1490. *type = ACPI_BUS_TYPE_THERMAL;
  1491. *sta = ACPI_STA_DEFAULT;
  1492. break;
  1493. case ACPI_TYPE_POWER:
  1494. *type = ACPI_BUS_TYPE_POWER;
  1495. *sta = ACPI_STA_DEFAULT;
  1496. break;
  1497. default:
  1498. return -ENODEV;
  1499. }
  1500. return 0;
  1501. }
  1502. bool acpi_device_is_present(const struct acpi_device *adev)
  1503. {
  1504. return adev->status.present || adev->status.functional;
  1505. }
  1506. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1507. const char *idstr,
  1508. const struct acpi_device_id **matchid)
  1509. {
  1510. const struct acpi_device_id *devid;
  1511. if (handler->match)
  1512. return handler->match(idstr, matchid);
  1513. for (devid = handler->ids; devid->id[0]; devid++)
  1514. if (!strcmp((char *)devid->id, idstr)) {
  1515. if (matchid)
  1516. *matchid = devid;
  1517. return true;
  1518. }
  1519. return false;
  1520. }
  1521. static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
  1522. const struct acpi_device_id **matchid)
  1523. {
  1524. struct acpi_scan_handler *handler;
  1525. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1526. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1527. return handler;
  1528. return NULL;
  1529. }
  1530. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1531. {
  1532. if (!!hotplug->enabled == !!val)
  1533. return;
  1534. mutex_lock(&acpi_scan_lock);
  1535. hotplug->enabled = val;
  1536. mutex_unlock(&acpi_scan_lock);
  1537. }
  1538. static void acpi_scan_init_hotplug(struct acpi_device *adev)
  1539. {
  1540. struct acpi_hardware_id *hwid;
  1541. if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
  1542. acpi_dock_add(adev);
  1543. return;
  1544. }
  1545. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  1546. struct acpi_scan_handler *handler;
  1547. handler = acpi_scan_match_handler(hwid->id, NULL);
  1548. if (handler) {
  1549. adev->flags.hotplug_notify = true;
  1550. break;
  1551. }
  1552. }
  1553. }
  1554. static void acpi_device_dep_initialize(struct acpi_device *adev)
  1555. {
  1556. struct acpi_dep_data *dep;
  1557. struct acpi_handle_list dep_devices;
  1558. acpi_status status;
  1559. int i;
  1560. adev->dep_unmet = 0;
  1561. if (!acpi_has_method(adev->handle, "_DEP"))
  1562. return;
  1563. status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
  1564. &dep_devices);
  1565. if (ACPI_FAILURE(status)) {
  1566. dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
  1567. return;
  1568. }
  1569. for (i = 0; i < dep_devices.count; i++) {
  1570. struct acpi_device_info *info;
  1571. int skip;
  1572. status = acpi_get_object_info(dep_devices.handles[i], &info);
  1573. if (ACPI_FAILURE(status)) {
  1574. dev_dbg(&adev->dev, "Error reading _DEP device info\n");
  1575. continue;
  1576. }
  1577. /*
  1578. * Skip the dependency of Windows System Power
  1579. * Management Controller
  1580. */
  1581. skip = info->valid & ACPI_VALID_HID &&
  1582. !strcmp(info->hardware_id.string, "INT3396");
  1583. kfree(info);
  1584. if (skip)
  1585. continue;
  1586. dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
  1587. if (!dep)
  1588. return;
  1589. dep->master = dep_devices.handles[i];
  1590. dep->slave = adev->handle;
  1591. adev->dep_unmet++;
  1592. mutex_lock(&acpi_dep_list_lock);
  1593. list_add_tail(&dep->node , &acpi_dep_list);
  1594. mutex_unlock(&acpi_dep_list_lock);
  1595. }
  1596. }
  1597. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1598. void *not_used, void **return_value)
  1599. {
  1600. struct acpi_device *device = NULL;
  1601. int type;
  1602. unsigned long long sta;
  1603. int result;
  1604. acpi_bus_get_device(handle, &device);
  1605. if (device)
  1606. goto out;
  1607. result = acpi_bus_type_and_status(handle, &type, &sta);
  1608. if (result)
  1609. return AE_OK;
  1610. if (type == ACPI_BUS_TYPE_POWER) {
  1611. acpi_add_power_resource(handle);
  1612. return AE_OK;
  1613. }
  1614. acpi_add_single_object(&device, handle, type, sta);
  1615. if (!device)
  1616. return AE_CTRL_DEPTH;
  1617. acpi_scan_init_hotplug(device);
  1618. acpi_device_dep_initialize(device);
  1619. out:
  1620. if (!*return_value)
  1621. *return_value = device;
  1622. return AE_OK;
  1623. }
  1624. static void acpi_default_enumeration(struct acpi_device *device)
  1625. {
  1626. /*
  1627. * Do not enumerate devices with enumeration_by_parent flag set as
  1628. * they will be enumerated by their respective parents.
  1629. */
  1630. if (!device->flags.enumeration_by_parent) {
  1631. acpi_create_platform_device(device, NULL);
  1632. acpi_device_set_enumerated(device);
  1633. } else {
  1634. blocking_notifier_call_chain(&acpi_reconfig_chain,
  1635. ACPI_RECONFIG_DEVICE_ADD, device);
  1636. }
  1637. }
  1638. static const struct acpi_device_id generic_device_ids[] = {
  1639. {ACPI_DT_NAMESPACE_HID, },
  1640. {"", },
  1641. };
  1642. static int acpi_generic_device_attach(struct acpi_device *adev,
  1643. const struct acpi_device_id *not_used)
  1644. {
  1645. /*
  1646. * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
  1647. * below can be unconditional.
  1648. */
  1649. if (adev->data.of_compatible)
  1650. acpi_default_enumeration(adev);
  1651. return 1;
  1652. }
  1653. static struct acpi_scan_handler generic_device_handler = {
  1654. .ids = generic_device_ids,
  1655. .attach = acpi_generic_device_attach,
  1656. };
  1657. static int acpi_scan_attach_handler(struct acpi_device *device)
  1658. {
  1659. struct acpi_hardware_id *hwid;
  1660. int ret = 0;
  1661. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1662. const struct acpi_device_id *devid;
  1663. struct acpi_scan_handler *handler;
  1664. handler = acpi_scan_match_handler(hwid->id, &devid);
  1665. if (handler) {
  1666. if (!handler->attach) {
  1667. device->pnp.type.platform_id = 0;
  1668. continue;
  1669. }
  1670. device->handler = handler;
  1671. ret = handler->attach(device, devid);
  1672. if (ret > 0)
  1673. break;
  1674. device->handler = NULL;
  1675. if (ret < 0)
  1676. break;
  1677. }
  1678. }
  1679. return ret;
  1680. }
  1681. static void acpi_bus_attach(struct acpi_device *device)
  1682. {
  1683. struct acpi_device *child;
  1684. acpi_handle ejd;
  1685. int ret;
  1686. if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
  1687. register_dock_dependent_device(device, ejd);
  1688. acpi_bus_get_status(device);
  1689. /* Skip devices that are not present. */
  1690. if (!acpi_device_is_present(device)) {
  1691. device->flags.initialized = false;
  1692. acpi_device_clear_enumerated(device);
  1693. device->flags.power_manageable = 0;
  1694. return;
  1695. }
  1696. if (device->handler)
  1697. goto ok;
  1698. if (!device->flags.initialized) {
  1699. device->flags.power_manageable =
  1700. device->power.states[ACPI_STATE_D0].flags.valid;
  1701. if (acpi_bus_init_power(device))
  1702. device->flags.power_manageable = 0;
  1703. device->flags.initialized = true;
  1704. } else if (device->flags.visited) {
  1705. goto ok;
  1706. }
  1707. ret = acpi_scan_attach_handler(device);
  1708. if (ret < 0)
  1709. return;
  1710. device->flags.match_driver = true;
  1711. if (ret > 0 && !device->flags.enumeration_by_parent) {
  1712. acpi_device_set_enumerated(device);
  1713. goto ok;
  1714. }
  1715. ret = device_attach(&device->dev);
  1716. if (ret < 0)
  1717. return;
  1718. if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
  1719. acpi_default_enumeration(device);
  1720. else
  1721. acpi_device_set_enumerated(device);
  1722. ok:
  1723. list_for_each_entry(child, &device->children, node)
  1724. acpi_bus_attach(child);
  1725. if (device->handler && device->handler->hotplug.notify_online)
  1726. device->handler->hotplug.notify_online(device);
  1727. }
  1728. void acpi_walk_dep_device_list(acpi_handle handle)
  1729. {
  1730. struct acpi_dep_data *dep, *tmp;
  1731. struct acpi_device *adev;
  1732. mutex_lock(&acpi_dep_list_lock);
  1733. list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
  1734. if (dep->master == handle) {
  1735. acpi_bus_get_device(dep->slave, &adev);
  1736. if (!adev)
  1737. continue;
  1738. adev->dep_unmet--;
  1739. if (!adev->dep_unmet)
  1740. acpi_bus_attach(adev);
  1741. list_del(&dep->node);
  1742. kfree(dep);
  1743. }
  1744. }
  1745. mutex_unlock(&acpi_dep_list_lock);
  1746. }
  1747. EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
  1748. /**
  1749. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1750. * @handle: Root of the namespace scope to scan.
  1751. *
  1752. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1753. * found devices.
  1754. *
  1755. * If no devices were found, -ENODEV is returned, but it does not mean that
  1756. * there has been a real error. There just have been no suitable ACPI objects
  1757. * in the table trunk from which the kernel could create a device and add an
  1758. * appropriate driver.
  1759. *
  1760. * Must be called under acpi_scan_lock.
  1761. */
  1762. int acpi_bus_scan(acpi_handle handle)
  1763. {
  1764. void *device = NULL;
  1765. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1766. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1767. acpi_bus_check_add, NULL, NULL, &device);
  1768. if (device) {
  1769. acpi_bus_attach(device);
  1770. return 0;
  1771. }
  1772. return -ENODEV;
  1773. }
  1774. EXPORT_SYMBOL(acpi_bus_scan);
  1775. /**
  1776. * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
  1777. * @adev: Root of the ACPI namespace scope to walk.
  1778. *
  1779. * Must be called under acpi_scan_lock.
  1780. */
  1781. void acpi_bus_trim(struct acpi_device *adev)
  1782. {
  1783. struct acpi_scan_handler *handler = adev->handler;
  1784. struct acpi_device *child;
  1785. list_for_each_entry_reverse(child, &adev->children, node)
  1786. acpi_bus_trim(child);
  1787. adev->flags.match_driver = false;
  1788. if (handler) {
  1789. if (handler->detach)
  1790. handler->detach(adev);
  1791. adev->handler = NULL;
  1792. } else {
  1793. device_release_driver(&adev->dev);
  1794. }
  1795. /*
  1796. * Most likely, the device is going away, so put it into D3cold before
  1797. * that.
  1798. */
  1799. acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
  1800. adev->flags.initialized = false;
  1801. acpi_device_clear_enumerated(adev);
  1802. }
  1803. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1804. int acpi_bus_register_early_device(int type)
  1805. {
  1806. struct acpi_device *device = NULL;
  1807. int result;
  1808. result = acpi_add_single_object(&device, NULL,
  1809. type, ACPI_STA_DEFAULT);
  1810. if (result)
  1811. return result;
  1812. device->flags.match_driver = true;
  1813. return device_attach(&device->dev);
  1814. }
  1815. EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
  1816. static int acpi_bus_scan_fixed(void)
  1817. {
  1818. int result = 0;
  1819. /*
  1820. * Enumerate all fixed-feature devices.
  1821. */
  1822. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1823. struct acpi_device *device = NULL;
  1824. result = acpi_add_single_object(&device, NULL,
  1825. ACPI_BUS_TYPE_POWER_BUTTON,
  1826. ACPI_STA_DEFAULT);
  1827. if (result)
  1828. return result;
  1829. device->flags.match_driver = true;
  1830. result = device_attach(&device->dev);
  1831. if (result < 0)
  1832. return result;
  1833. device_init_wakeup(&device->dev, true);
  1834. }
  1835. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1836. struct acpi_device *device = NULL;
  1837. result = acpi_add_single_object(&device, NULL,
  1838. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1839. ACPI_STA_DEFAULT);
  1840. if (result)
  1841. return result;
  1842. device->flags.match_driver = true;
  1843. result = device_attach(&device->dev);
  1844. }
  1845. return result < 0 ? result : 0;
  1846. }
  1847. static void __init acpi_get_spcr_uart_addr(void)
  1848. {
  1849. acpi_status status;
  1850. struct acpi_table_spcr *spcr_ptr;
  1851. status = acpi_get_table(ACPI_SIG_SPCR, 0,
  1852. (struct acpi_table_header **)&spcr_ptr);
  1853. if (ACPI_SUCCESS(status))
  1854. spcr_uart_addr = spcr_ptr->serial_port.address;
  1855. else
  1856. printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
  1857. }
  1858. static bool acpi_scan_initialized;
  1859. int __init acpi_scan_init(void)
  1860. {
  1861. int result;
  1862. acpi_status status;
  1863. struct acpi_table_stao *stao_ptr;
  1864. acpi_pci_root_init();
  1865. acpi_pci_link_init();
  1866. acpi_processor_init();
  1867. acpi_lpss_init();
  1868. acpi_apd_init();
  1869. acpi_cmos_rtc_init();
  1870. acpi_container_init();
  1871. acpi_memory_hotplug_init();
  1872. acpi_watchdog_init();
  1873. acpi_pnp_init();
  1874. acpi_int340x_thermal_init();
  1875. acpi_amba_init();
  1876. acpi_init_lpit();
  1877. acpi_scan_add_handler(&generic_device_handler);
  1878. /*
  1879. * If there is STAO table, check whether it needs to ignore the UART
  1880. * device in SPCR table.
  1881. */
  1882. status = acpi_get_table(ACPI_SIG_STAO, 0,
  1883. (struct acpi_table_header **)&stao_ptr);
  1884. if (ACPI_SUCCESS(status)) {
  1885. if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
  1886. printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
  1887. if (stao_ptr->ignore_uart)
  1888. acpi_get_spcr_uart_addr();
  1889. }
  1890. acpi_gpe_apply_masked_gpes();
  1891. acpi_update_all_gpes();
  1892. mutex_lock(&acpi_scan_lock);
  1893. /*
  1894. * Enumerate devices in the ACPI namespace.
  1895. */
  1896. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1897. if (result)
  1898. goto out;
  1899. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1900. if (result)
  1901. goto out;
  1902. /* Fixed feature devices do not exist on HW-reduced platform */
  1903. if (!acpi_gbl_reduced_hardware) {
  1904. result = acpi_bus_scan_fixed();
  1905. if (result) {
  1906. acpi_detach_data(acpi_root->handle,
  1907. acpi_scan_drop_device);
  1908. acpi_device_del(acpi_root);
  1909. put_device(&acpi_root->dev);
  1910. goto out;
  1911. }
  1912. }
  1913. acpi_scan_initialized = true;
  1914. out:
  1915. mutex_unlock(&acpi_scan_lock);
  1916. return result;
  1917. }
  1918. static struct acpi_probe_entry *ape;
  1919. static int acpi_probe_count;
  1920. static DEFINE_MUTEX(acpi_probe_mutex);
  1921. static int __init acpi_match_madt(struct acpi_subtable_header *header,
  1922. const unsigned long end)
  1923. {
  1924. if (!ape->subtable_valid || ape->subtable_valid(header, ape))
  1925. if (!ape->probe_subtbl(header, end))
  1926. acpi_probe_count++;
  1927. return 0;
  1928. }
  1929. int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
  1930. {
  1931. int count = 0;
  1932. if (acpi_disabled)
  1933. return 0;
  1934. mutex_lock(&acpi_probe_mutex);
  1935. for (ape = ap_head; nr; ape++, nr--) {
  1936. if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
  1937. acpi_probe_count = 0;
  1938. acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
  1939. count += acpi_probe_count;
  1940. } else {
  1941. int res;
  1942. res = acpi_table_parse(ape->id, ape->probe_table);
  1943. if (!res)
  1944. count++;
  1945. }
  1946. }
  1947. mutex_unlock(&acpi_probe_mutex);
  1948. return count;
  1949. }
  1950. struct acpi_table_events_work {
  1951. struct work_struct work;
  1952. void *table;
  1953. u32 event;
  1954. };
  1955. static void acpi_table_events_fn(struct work_struct *work)
  1956. {
  1957. struct acpi_table_events_work *tew;
  1958. tew = container_of(work, struct acpi_table_events_work, work);
  1959. if (tew->event == ACPI_TABLE_EVENT_LOAD) {
  1960. acpi_scan_lock_acquire();
  1961. acpi_bus_scan(ACPI_ROOT_OBJECT);
  1962. acpi_scan_lock_release();
  1963. }
  1964. kfree(tew);
  1965. }
  1966. void acpi_scan_table_handler(u32 event, void *table, void *context)
  1967. {
  1968. struct acpi_table_events_work *tew;
  1969. if (!acpi_scan_initialized)
  1970. return;
  1971. if (event != ACPI_TABLE_EVENT_LOAD)
  1972. return;
  1973. tew = kmalloc(sizeof(*tew), GFP_KERNEL);
  1974. if (!tew)
  1975. return;
  1976. INIT_WORK(&tew->work, acpi_table_events_fn);
  1977. tew->table = table;
  1978. tew->event = event;
  1979. schedule_work(&tew->work);
  1980. }
  1981. int acpi_reconfig_notifier_register(struct notifier_block *nb)
  1982. {
  1983. return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
  1984. }
  1985. EXPORT_SYMBOL(acpi_reconfig_notifier_register);
  1986. int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
  1987. {
  1988. return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
  1989. }
  1990. EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);