scan.c 54 KB

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