scan.c 58 KB

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