scan.c 57 KB

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