scan.c 48 KB

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