scan.c 54 KB

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