scan.c 56 KB

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