scan.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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 <acpi/acpi_drivers.h>
  14. #include "internal.h"
  15. #define _COMPONENT ACPI_BUS_COMPONENT
  16. ACPI_MODULE_NAME("scan");
  17. #define STRUCT_TO_INT(s) (*((int*)&s))
  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. /*
  24. * If set, devices will be hot-removed even if they cannot be put offline
  25. * gracefully (from the kernel's standpoint).
  26. */
  27. bool acpi_force_hot_remove;
  28. static const char *dummy_hid = "device";
  29. static 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. struct acpi_device_bus_id{
  35. char bus_id[15];
  36. unsigned int instance_no;
  37. struct list_head node;
  38. };
  39. void acpi_scan_lock_acquire(void)
  40. {
  41. mutex_lock(&acpi_scan_lock);
  42. }
  43. EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
  44. void acpi_scan_lock_release(void)
  45. {
  46. mutex_unlock(&acpi_scan_lock);
  47. }
  48. EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
  49. int acpi_scan_add_handler(struct acpi_scan_handler *handler)
  50. {
  51. if (!handler || !handler->attach)
  52. return -EINVAL;
  53. list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
  54. return 0;
  55. }
  56. int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
  57. const char *hotplug_profile_name)
  58. {
  59. int error;
  60. error = acpi_scan_add_handler(handler);
  61. if (error)
  62. return error;
  63. acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
  64. return 0;
  65. }
  66. /*
  67. * Creates hid/cid(s) string needed for modalias and uevent
  68. * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  69. * char *modalias: "acpi:IBM0001:ACPI0001"
  70. */
  71. static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
  72. int size)
  73. {
  74. int len;
  75. int count;
  76. struct acpi_hardware_id *id;
  77. if (list_empty(&acpi_dev->pnp.ids))
  78. return 0;
  79. len = snprintf(modalias, size, "acpi:");
  80. size -= len;
  81. list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
  82. count = snprintf(&modalias[len], size, "%s:", id->id);
  83. if (count < 0 || count >= size)
  84. return -EINVAL;
  85. len += count;
  86. size -= count;
  87. }
  88. modalias[len] = '\0';
  89. return len;
  90. }
  91. static ssize_t
  92. acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
  93. struct acpi_device *acpi_dev = to_acpi_device(dev);
  94. int len;
  95. /* Device has no HID and no CID or string is >1024 */
  96. len = create_modalias(acpi_dev, buf, 1024);
  97. if (len <= 0)
  98. return 0;
  99. buf[len++] = '\n';
  100. return len;
  101. }
  102. static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
  103. static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
  104. void **ret_p)
  105. {
  106. struct acpi_device *device = NULL;
  107. struct acpi_device_physical_node *pn;
  108. bool second_pass = (bool)data;
  109. acpi_status status = AE_OK;
  110. if (acpi_bus_get_device(handle, &device))
  111. return AE_OK;
  112. if (device->handler && !device->handler->hotplug.enabled) {
  113. *ret_p = &device->dev;
  114. return AE_SUPPORT;
  115. }
  116. mutex_lock(&device->physical_node_lock);
  117. list_for_each_entry(pn, &device->physical_node_list, node) {
  118. int ret;
  119. if (second_pass) {
  120. /* Skip devices offlined by the first pass. */
  121. if (pn->put_online)
  122. continue;
  123. } else {
  124. pn->put_online = false;
  125. }
  126. ret = device_offline(pn->dev);
  127. if (acpi_force_hot_remove)
  128. continue;
  129. if (ret >= 0) {
  130. pn->put_online = !ret;
  131. } else {
  132. *ret_p = pn->dev;
  133. if (second_pass) {
  134. status = AE_ERROR;
  135. break;
  136. }
  137. }
  138. }
  139. mutex_unlock(&device->physical_node_lock);
  140. return status;
  141. }
  142. static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
  143. void **ret_p)
  144. {
  145. struct acpi_device *device = NULL;
  146. struct acpi_device_physical_node *pn;
  147. if (acpi_bus_get_device(handle, &device))
  148. return AE_OK;
  149. mutex_lock(&device->physical_node_lock);
  150. list_for_each_entry(pn, &device->physical_node_list, node)
  151. if (pn->put_online) {
  152. device_online(pn->dev);
  153. pn->put_online = false;
  154. }
  155. mutex_unlock(&device->physical_node_lock);
  156. return AE_OK;
  157. }
  158. static int acpi_scan_hot_remove(struct acpi_device *device)
  159. {
  160. acpi_handle handle = device->handle;
  161. struct device *errdev;
  162. acpi_status status;
  163. unsigned long long sta;
  164. /* If there is no handle, the device node has been unregistered. */
  165. if (!handle) {
  166. dev_dbg(&device->dev, "ACPI handle missing\n");
  167. put_device(&device->dev);
  168. return -EINVAL;
  169. }
  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. errdev = NULL;
  180. status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  181. NULL, acpi_bus_offline, (void *)false,
  182. (void **)&errdev);
  183. if (status == AE_SUPPORT) {
  184. dev_warn(errdev, "Offline disabled.\n");
  185. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  186. acpi_bus_online, NULL, NULL, NULL);
  187. put_device(&device->dev);
  188. return -EPERM;
  189. }
  190. acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
  191. if (errdev) {
  192. errdev = NULL;
  193. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  194. NULL, acpi_bus_offline, (void *)true,
  195. (void **)&errdev);
  196. if (!errdev || acpi_force_hot_remove)
  197. acpi_bus_offline(handle, 0, (void *)true,
  198. (void **)&errdev);
  199. if (errdev && !acpi_force_hot_remove) {
  200. dev_warn(errdev, "Offline failed.\n");
  201. acpi_bus_online(handle, 0, NULL, NULL);
  202. acpi_walk_namespace(ACPI_TYPE_ANY, handle,
  203. ACPI_UINT32_MAX, acpi_bus_online,
  204. NULL, NULL, NULL);
  205. put_device(&device->dev);
  206. return -EBUSY;
  207. }
  208. }
  209. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  210. "Hot-removing device %s...\n", dev_name(&device->dev)));
  211. acpi_bus_trim(device);
  212. /* Device node has been unregistered. */
  213. put_device(&device->dev);
  214. device = NULL;
  215. acpi_evaluate_lck(handle, 0);
  216. /*
  217. * TBD: _EJD support.
  218. */
  219. status = acpi_evaluate_ej0(handle);
  220. if (status == AE_NOT_FOUND)
  221. return -ENODEV;
  222. else if (ACPI_FAILURE(status))
  223. return -EIO;
  224. /*
  225. * Verify if eject was indeed successful. If not, log an error
  226. * message. No need to call _OST since _EJ0 call was made OK.
  227. */
  228. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  229. if (ACPI_FAILURE(status)) {
  230. acpi_handle_warn(handle,
  231. "Status check after eject failed (0x%x)\n", status);
  232. } else if (sta & ACPI_STA_DEVICE_ENABLED) {
  233. acpi_handle_warn(handle,
  234. "Eject incomplete - status 0x%llx\n", sta);
  235. }
  236. return 0;
  237. }
  238. void acpi_bus_device_eject(struct acpi_device *device, u32 ost_src)
  239. {
  240. acpi_handle handle = device->handle;
  241. struct acpi_scan_handler *handler;
  242. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  243. int error;
  244. lock_device_hotplug();
  245. mutex_lock(&acpi_scan_lock);
  246. handler = device->handler;
  247. if (!handler || !handler->hotplug.enabled) {
  248. put_device(&device->dev);
  249. goto err_support;
  250. }
  251. if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
  252. acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
  253. ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
  254. if (handler->hotplug.mode == AHM_CONTAINER)
  255. kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
  256. error = acpi_scan_hot_remove(device);
  257. if (error == -EPERM) {
  258. goto err_support;
  259. } else if (error) {
  260. goto err_out;
  261. }
  262. out:
  263. mutex_unlock(&acpi_scan_lock);
  264. unlock_device_hotplug();
  265. return;
  266. err_support:
  267. ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
  268. err_out:
  269. acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
  270. goto out;
  271. }
  272. static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
  273. {
  274. struct acpi_device *device = NULL;
  275. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  276. int error;
  277. lock_device_hotplug();
  278. mutex_lock(&acpi_scan_lock);
  279. if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
  280. acpi_bus_get_device(handle, &device);
  281. if (device) {
  282. dev_warn(&device->dev, "Attempt to re-insert\n");
  283. goto out;
  284. }
  285. }
  286. acpi_evaluate_hotplug_ost(handle, ost_source,
  287. ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
  288. error = acpi_bus_scan(handle);
  289. if (error) {
  290. acpi_handle_warn(handle, "Namespace scan failure\n");
  291. goto out;
  292. }
  293. error = acpi_bus_get_device(handle, &device);
  294. if (error) {
  295. acpi_handle_warn(handle, "Missing device node object\n");
  296. goto out;
  297. }
  298. ost_code = ACPI_OST_SC_SUCCESS;
  299. if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
  300. kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
  301. out:
  302. acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
  303. mutex_unlock(&acpi_scan_lock);
  304. unlock_device_hotplug();
  305. }
  306. static void acpi_scan_bus_check(void *context)
  307. {
  308. acpi_scan_bus_device_check((acpi_handle)context,
  309. ACPI_NOTIFY_BUS_CHECK);
  310. }
  311. static void acpi_scan_device_check(void *context)
  312. {
  313. acpi_scan_bus_device_check((acpi_handle)context,
  314. ACPI_NOTIFY_DEVICE_CHECK);
  315. }
  316. static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
  317. {
  318. u32 ost_status;
  319. switch (type) {
  320. case ACPI_NOTIFY_BUS_CHECK:
  321. acpi_handle_debug(handle,
  322. "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
  323. ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
  324. break;
  325. case ACPI_NOTIFY_DEVICE_CHECK:
  326. acpi_handle_debug(handle,
  327. "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
  328. ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
  329. break;
  330. case ACPI_NOTIFY_EJECT_REQUEST:
  331. acpi_handle_debug(handle,
  332. "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
  333. ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
  334. break;
  335. default:
  336. /* non-hotplug event; possibly handled by other handler */
  337. return;
  338. }
  339. acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
  340. }
  341. /**
  342. * acpi_bus_hot_remove_device: Hot-remove a device and its children.
  343. * @context: Address of the ACPI device object to hot-remove.
  344. */
  345. static void acpi_bus_hot_remove_device(void *context)
  346. {
  347. acpi_bus_device_eject(context, ACPI_NOTIFY_EJECT_REQUEST);
  348. }
  349. static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
  350. {
  351. acpi_osd_exec_callback callback;
  352. struct acpi_scan_handler *handler = data;
  353. struct acpi_device *adev;
  354. acpi_status status;
  355. if (!handler->hotplug.enabled)
  356. return acpi_hotplug_unsupported(handle, type);
  357. switch (type) {
  358. case ACPI_NOTIFY_BUS_CHECK:
  359. acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
  360. callback = acpi_scan_bus_check;
  361. break;
  362. case ACPI_NOTIFY_DEVICE_CHECK:
  363. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
  364. callback = acpi_scan_device_check;
  365. break;
  366. case ACPI_NOTIFY_EJECT_REQUEST:
  367. acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
  368. status = acpi_bus_get_device(handle, &adev);
  369. if (ACPI_FAILURE(status))
  370. goto err_out;
  371. get_device(&adev->dev);
  372. callback = acpi_bus_hot_remove_device;
  373. status = acpi_os_hotplug_execute(callback, adev);
  374. if (ACPI_SUCCESS(status))
  375. return;
  376. put_device(&adev->dev);
  377. goto err_out;
  378. default:
  379. /* non-hotplug event; possibly handled by other handler */
  380. return;
  381. }
  382. status = acpi_os_hotplug_execute(callback, handle);
  383. if (ACPI_SUCCESS(status))
  384. return;
  385. err_out:
  386. acpi_evaluate_hotplug_ost(handle, type,
  387. ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
  388. }
  389. static ssize_t real_power_state_show(struct device *dev,
  390. struct device_attribute *attr, char *buf)
  391. {
  392. struct acpi_device *adev = to_acpi_device(dev);
  393. int state;
  394. int ret;
  395. ret = acpi_device_get_power(adev, &state);
  396. if (ret)
  397. return ret;
  398. return sprintf(buf, "%s\n", acpi_power_state_string(state));
  399. }
  400. static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
  401. static ssize_t power_state_show(struct device *dev,
  402. struct device_attribute *attr, char *buf)
  403. {
  404. struct acpi_device *adev = to_acpi_device(dev);
  405. return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
  406. }
  407. static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
  408. static void acpi_eject_store_work(void *context)
  409. {
  410. acpi_bus_device_eject(context, ACPI_OST_EC_OSPM_EJECT);
  411. }
  412. static ssize_t
  413. acpi_eject_store(struct device *d, struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct acpi_device *acpi_device = to_acpi_device(d);
  417. acpi_object_type not_used;
  418. acpi_status status;
  419. if (!count || buf[0] != '1')
  420. return -EINVAL;
  421. if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
  422. && !acpi_device->driver)
  423. return -ENODEV;
  424. status = acpi_get_type(acpi_device->handle, &not_used);
  425. if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
  426. return -ENODEV;
  427. acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
  428. ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
  429. get_device(&acpi_device->dev);
  430. status = acpi_os_hotplug_execute(acpi_eject_store_work, acpi_device);
  431. if (ACPI_SUCCESS(status))
  432. return count;
  433. put_device(&acpi_device->dev);
  434. acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
  435. ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
  436. return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
  437. }
  438. static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
  439. static ssize_t
  440. acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
  441. struct acpi_device *acpi_dev = to_acpi_device(dev);
  442. return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
  443. }
  444. static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
  445. static ssize_t acpi_device_uid_show(struct device *dev,
  446. struct device_attribute *attr, char *buf)
  447. {
  448. struct acpi_device *acpi_dev = to_acpi_device(dev);
  449. return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
  450. }
  451. static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
  452. static ssize_t acpi_device_adr_show(struct device *dev,
  453. struct device_attribute *attr, char *buf)
  454. {
  455. struct acpi_device *acpi_dev = to_acpi_device(dev);
  456. return sprintf(buf, "0x%08x\n",
  457. (unsigned int)(acpi_dev->pnp.bus_address));
  458. }
  459. static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
  460. static ssize_t
  461. acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
  462. struct acpi_device *acpi_dev = to_acpi_device(dev);
  463. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  464. int result;
  465. result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
  466. if (result)
  467. goto end;
  468. result = sprintf(buf, "%s\n", (char*)path.pointer);
  469. kfree(path.pointer);
  470. end:
  471. return result;
  472. }
  473. static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
  474. /* sysfs file that shows description text from the ACPI _STR method */
  475. static ssize_t description_show(struct device *dev,
  476. struct device_attribute *attr,
  477. char *buf) {
  478. struct acpi_device *acpi_dev = to_acpi_device(dev);
  479. int result;
  480. if (acpi_dev->pnp.str_obj == NULL)
  481. return 0;
  482. /*
  483. * The _STR object contains a Unicode identifier for a device.
  484. * We need to convert to utf-8 so it can be displayed.
  485. */
  486. result = utf16s_to_utf8s(
  487. (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
  488. acpi_dev->pnp.str_obj->buffer.length,
  489. UTF16_LITTLE_ENDIAN, buf,
  490. PAGE_SIZE);
  491. buf[result++] = '\n';
  492. return result;
  493. }
  494. static DEVICE_ATTR(description, 0444, description_show, NULL);
  495. static ssize_t
  496. acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
  497. char *buf) {
  498. struct acpi_device *acpi_dev = to_acpi_device(dev);
  499. return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
  500. }
  501. static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
  502. static int acpi_device_setup_files(struct acpi_device *dev)
  503. {
  504. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  505. acpi_status status;
  506. unsigned long long sun;
  507. int result = 0;
  508. /*
  509. * Devices gotten from FADT don't have a "path" attribute
  510. */
  511. if (dev->handle) {
  512. result = device_create_file(&dev->dev, &dev_attr_path);
  513. if (result)
  514. goto end;
  515. }
  516. if (!list_empty(&dev->pnp.ids)) {
  517. result = device_create_file(&dev->dev, &dev_attr_hid);
  518. if (result)
  519. goto end;
  520. result = device_create_file(&dev->dev, &dev_attr_modalias);
  521. if (result)
  522. goto end;
  523. }
  524. /*
  525. * If device has _STR, 'description' file is created
  526. */
  527. if (acpi_has_method(dev->handle, "_STR")) {
  528. status = acpi_evaluate_object(dev->handle, "_STR",
  529. NULL, &buffer);
  530. if (ACPI_FAILURE(status))
  531. buffer.pointer = NULL;
  532. dev->pnp.str_obj = buffer.pointer;
  533. result = device_create_file(&dev->dev, &dev_attr_description);
  534. if (result)
  535. goto end;
  536. }
  537. if (dev->pnp.type.bus_address)
  538. result = device_create_file(&dev->dev, &dev_attr_adr);
  539. if (dev->pnp.unique_id)
  540. result = device_create_file(&dev->dev, &dev_attr_uid);
  541. status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
  542. if (ACPI_SUCCESS(status)) {
  543. dev->pnp.sun = (unsigned long)sun;
  544. result = device_create_file(&dev->dev, &dev_attr_sun);
  545. if (result)
  546. goto end;
  547. } else {
  548. dev->pnp.sun = (unsigned long)-1;
  549. }
  550. /*
  551. * If device has _EJ0, 'eject' file is created that is used to trigger
  552. * hot-removal function from userland.
  553. */
  554. if (acpi_has_method(dev->handle, "_EJ0")) {
  555. result = device_create_file(&dev->dev, &dev_attr_eject);
  556. if (result)
  557. return result;
  558. }
  559. if (dev->flags.power_manageable) {
  560. result = device_create_file(&dev->dev, &dev_attr_power_state);
  561. if (result)
  562. return result;
  563. if (dev->power.flags.power_resources)
  564. result = device_create_file(&dev->dev,
  565. &dev_attr_real_power_state);
  566. }
  567. end:
  568. return result;
  569. }
  570. static void acpi_device_remove_files(struct acpi_device *dev)
  571. {
  572. if (dev->flags.power_manageable) {
  573. device_remove_file(&dev->dev, &dev_attr_power_state);
  574. if (dev->power.flags.power_resources)
  575. device_remove_file(&dev->dev,
  576. &dev_attr_real_power_state);
  577. }
  578. /*
  579. * If device has _STR, remove 'description' file
  580. */
  581. if (acpi_has_method(dev->handle, "_STR")) {
  582. kfree(dev->pnp.str_obj);
  583. device_remove_file(&dev->dev, &dev_attr_description);
  584. }
  585. /*
  586. * If device has _EJ0, remove 'eject' file.
  587. */
  588. if (acpi_has_method(dev->handle, "_EJ0"))
  589. device_remove_file(&dev->dev, &dev_attr_eject);
  590. if (acpi_has_method(dev->handle, "_SUN"))
  591. device_remove_file(&dev->dev, &dev_attr_sun);
  592. if (dev->pnp.unique_id)
  593. device_remove_file(&dev->dev, &dev_attr_uid);
  594. if (dev->pnp.type.bus_address)
  595. device_remove_file(&dev->dev, &dev_attr_adr);
  596. device_remove_file(&dev->dev, &dev_attr_modalias);
  597. device_remove_file(&dev->dev, &dev_attr_hid);
  598. if (dev->handle)
  599. device_remove_file(&dev->dev, &dev_attr_path);
  600. }
  601. /* --------------------------------------------------------------------------
  602. ACPI Bus operations
  603. -------------------------------------------------------------------------- */
  604. static const struct acpi_device_id *__acpi_match_device(
  605. struct acpi_device *device, const struct acpi_device_id *ids)
  606. {
  607. const struct acpi_device_id *id;
  608. struct acpi_hardware_id *hwid;
  609. /*
  610. * If the device is not present, it is unnecessary to load device
  611. * driver for it.
  612. */
  613. if (!device->status.present)
  614. return NULL;
  615. for (id = ids; id->id[0]; id++)
  616. list_for_each_entry(hwid, &device->pnp.ids, list)
  617. if (!strcmp((char *) id->id, hwid->id))
  618. return id;
  619. return NULL;
  620. }
  621. /**
  622. * acpi_match_device - Match a struct device against a given list of ACPI IDs
  623. * @ids: Array of struct acpi_device_id object to match against.
  624. * @dev: The device structure to match.
  625. *
  626. * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
  627. * object for that handle and use that object to match against a given list of
  628. * device IDs.
  629. *
  630. * Return a pointer to the first matching ID on success or %NULL on failure.
  631. */
  632. const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
  633. const struct device *dev)
  634. {
  635. struct acpi_device *adev;
  636. acpi_handle handle = ACPI_HANDLE(dev);
  637. if (!ids || !handle || acpi_bus_get_device(handle, &adev))
  638. return NULL;
  639. return __acpi_match_device(adev, ids);
  640. }
  641. EXPORT_SYMBOL_GPL(acpi_match_device);
  642. int acpi_match_device_ids(struct acpi_device *device,
  643. const struct acpi_device_id *ids)
  644. {
  645. return __acpi_match_device(device, ids) ? 0 : -ENOENT;
  646. }
  647. EXPORT_SYMBOL(acpi_match_device_ids);
  648. static void acpi_free_power_resources_lists(struct acpi_device *device)
  649. {
  650. int i;
  651. if (device->wakeup.flags.valid)
  652. acpi_power_resources_list_free(&device->wakeup.resources);
  653. if (!device->flags.power_manageable)
  654. return;
  655. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  656. struct acpi_device_power_state *ps = &device->power.states[i];
  657. acpi_power_resources_list_free(&ps->resources);
  658. }
  659. }
  660. static void acpi_device_release(struct device *dev)
  661. {
  662. struct acpi_device *acpi_dev = to_acpi_device(dev);
  663. acpi_free_pnp_ids(&acpi_dev->pnp);
  664. acpi_free_power_resources_lists(acpi_dev);
  665. kfree(acpi_dev);
  666. }
  667. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  668. {
  669. struct acpi_device *acpi_dev = to_acpi_device(dev);
  670. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  671. return acpi_dev->flags.match_driver
  672. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  673. }
  674. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  675. {
  676. struct acpi_device *acpi_dev = to_acpi_device(dev);
  677. int len;
  678. if (list_empty(&acpi_dev->pnp.ids))
  679. return 0;
  680. if (add_uevent_var(env, "MODALIAS="))
  681. return -ENOMEM;
  682. len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
  683. sizeof(env->buf) - env->buflen);
  684. if (len >= (sizeof(env->buf) - env->buflen))
  685. return -ENOMEM;
  686. env->buflen += len;
  687. return 0;
  688. }
  689. static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
  690. {
  691. struct acpi_device *device = data;
  692. device->driver->ops.notify(device, event);
  693. }
  694. static acpi_status acpi_device_notify_fixed(void *data)
  695. {
  696. struct acpi_device *device = data;
  697. /* Fixed hardware devices have no handles */
  698. acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
  699. return AE_OK;
  700. }
  701. static int acpi_device_install_notify_handler(struct acpi_device *device)
  702. {
  703. acpi_status status;
  704. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  705. status =
  706. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  707. acpi_device_notify_fixed,
  708. device);
  709. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  710. status =
  711. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  712. acpi_device_notify_fixed,
  713. device);
  714. else
  715. status = acpi_install_notify_handler(device->handle,
  716. ACPI_DEVICE_NOTIFY,
  717. acpi_device_notify,
  718. device);
  719. if (ACPI_FAILURE(status))
  720. return -EINVAL;
  721. return 0;
  722. }
  723. static void acpi_device_remove_notify_handler(struct acpi_device *device)
  724. {
  725. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  726. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  727. acpi_device_notify_fixed);
  728. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  729. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  730. acpi_device_notify_fixed);
  731. else
  732. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  733. acpi_device_notify);
  734. }
  735. static int acpi_device_probe(struct device *dev)
  736. {
  737. struct acpi_device *acpi_dev = to_acpi_device(dev);
  738. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  739. int ret;
  740. if (acpi_dev->handler)
  741. return -EINVAL;
  742. if (!acpi_drv->ops.add)
  743. return -ENOSYS;
  744. ret = acpi_drv->ops.add(acpi_dev);
  745. if (ret)
  746. return ret;
  747. acpi_dev->driver = acpi_drv;
  748. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  749. "Driver [%s] successfully bound to device [%s]\n",
  750. acpi_drv->name, acpi_dev->pnp.bus_id));
  751. if (acpi_drv->ops.notify) {
  752. ret = acpi_device_install_notify_handler(acpi_dev);
  753. if (ret) {
  754. if (acpi_drv->ops.remove)
  755. acpi_drv->ops.remove(acpi_dev);
  756. acpi_dev->driver = NULL;
  757. acpi_dev->driver_data = NULL;
  758. return ret;
  759. }
  760. }
  761. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
  762. acpi_drv->name, acpi_dev->pnp.bus_id));
  763. get_device(dev);
  764. return 0;
  765. }
  766. static int acpi_device_remove(struct device * dev)
  767. {
  768. struct acpi_device *acpi_dev = to_acpi_device(dev);
  769. struct acpi_driver *acpi_drv = acpi_dev->driver;
  770. if (acpi_drv) {
  771. if (acpi_drv->ops.notify)
  772. acpi_device_remove_notify_handler(acpi_dev);
  773. if (acpi_drv->ops.remove)
  774. acpi_drv->ops.remove(acpi_dev);
  775. }
  776. acpi_dev->driver = NULL;
  777. acpi_dev->driver_data = NULL;
  778. put_device(dev);
  779. return 0;
  780. }
  781. struct bus_type acpi_bus_type = {
  782. .name = "acpi",
  783. .match = acpi_bus_match,
  784. .probe = acpi_device_probe,
  785. .remove = acpi_device_remove,
  786. .uevent = acpi_device_uevent,
  787. };
  788. static void acpi_bus_data_handler(acpi_handle handle, void *context)
  789. {
  790. /* Intentionally empty. */
  791. }
  792. int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
  793. {
  794. acpi_status status;
  795. if (!device)
  796. return -EINVAL;
  797. status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
  798. if (ACPI_FAILURE(status) || !*device) {
  799. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  800. handle));
  801. return -ENODEV;
  802. }
  803. return 0;
  804. }
  805. EXPORT_SYMBOL_GPL(acpi_bus_get_device);
  806. int acpi_device_add(struct acpi_device *device,
  807. void (*release)(struct device *))
  808. {
  809. int result;
  810. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  811. int found = 0;
  812. if (device->handle) {
  813. acpi_status status;
  814. status = acpi_attach_data(device->handle, acpi_bus_data_handler,
  815. device);
  816. if (ACPI_FAILURE(status)) {
  817. acpi_handle_err(device->handle,
  818. "Unable to attach device data\n");
  819. return -ENODEV;
  820. }
  821. }
  822. /*
  823. * Linkage
  824. * -------
  825. * Link this device to its parent and siblings.
  826. */
  827. INIT_LIST_HEAD(&device->children);
  828. INIT_LIST_HEAD(&device->node);
  829. INIT_LIST_HEAD(&device->wakeup_list);
  830. INIT_LIST_HEAD(&device->physical_node_list);
  831. mutex_init(&device->physical_node_lock);
  832. INIT_LIST_HEAD(&device->power_dependent);
  833. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  834. if (!new_bus_id) {
  835. pr_err(PREFIX "Memory allocation error\n");
  836. result = -ENOMEM;
  837. goto err_detach;
  838. }
  839. mutex_lock(&acpi_device_lock);
  840. /*
  841. * Find suitable bus_id and instance number in acpi_bus_id_list
  842. * If failed, create one and link it into acpi_bus_id_list
  843. */
  844. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  845. if (!strcmp(acpi_device_bus_id->bus_id,
  846. acpi_device_hid(device))) {
  847. acpi_device_bus_id->instance_no++;
  848. found = 1;
  849. kfree(new_bus_id);
  850. break;
  851. }
  852. }
  853. if (!found) {
  854. acpi_device_bus_id = new_bus_id;
  855. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  856. acpi_device_bus_id->instance_no = 0;
  857. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  858. }
  859. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  860. if (device->parent)
  861. list_add_tail(&device->node, &device->parent->children);
  862. if (device->wakeup.flags.valid)
  863. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  864. mutex_unlock(&acpi_device_lock);
  865. if (device->parent)
  866. device->dev.parent = &device->parent->dev;
  867. device->dev.bus = &acpi_bus_type;
  868. device->dev.release = release;
  869. result = device_add(&device->dev);
  870. if (result) {
  871. dev_err(&device->dev, "Error registering device\n");
  872. goto err;
  873. }
  874. result = acpi_device_setup_files(device);
  875. if (result)
  876. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  877. dev_name(&device->dev));
  878. return 0;
  879. err:
  880. mutex_lock(&acpi_device_lock);
  881. if (device->parent)
  882. list_del(&device->node);
  883. list_del(&device->wakeup_list);
  884. mutex_unlock(&acpi_device_lock);
  885. err_detach:
  886. acpi_detach_data(device->handle, acpi_bus_data_handler);
  887. return result;
  888. }
  889. static void acpi_device_unregister(struct acpi_device *device)
  890. {
  891. mutex_lock(&acpi_device_lock);
  892. if (device->parent)
  893. list_del(&device->node);
  894. list_del(&device->wakeup_list);
  895. mutex_unlock(&acpi_device_lock);
  896. acpi_detach_data(device->handle, acpi_bus_data_handler);
  897. acpi_power_add_remove_device(device, false);
  898. acpi_device_remove_files(device);
  899. if (device->remove)
  900. device->remove(device);
  901. device_del(&device->dev);
  902. /*
  903. * Transition the device to D3cold to drop the reference counts of all
  904. * power resources the device depends on and turn off the ones that have
  905. * no more references.
  906. */
  907. acpi_device_set_power(device, ACPI_STATE_D3_COLD);
  908. device->handle = NULL;
  909. put_device(&device->dev);
  910. }
  911. /* --------------------------------------------------------------------------
  912. Driver Management
  913. -------------------------------------------------------------------------- */
  914. /**
  915. * acpi_bus_register_driver - register a driver with the ACPI bus
  916. * @driver: driver being registered
  917. *
  918. * Registers a driver with the ACPI bus. Searches the namespace for all
  919. * devices that match the driver's criteria and binds. Returns zero for
  920. * success or a negative error status for failure.
  921. */
  922. int acpi_bus_register_driver(struct acpi_driver *driver)
  923. {
  924. int ret;
  925. if (acpi_disabled)
  926. return -ENODEV;
  927. driver->drv.name = driver->name;
  928. driver->drv.bus = &acpi_bus_type;
  929. driver->drv.owner = driver->owner;
  930. ret = driver_register(&driver->drv);
  931. return ret;
  932. }
  933. EXPORT_SYMBOL(acpi_bus_register_driver);
  934. /**
  935. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  936. * @driver: driver to unregister
  937. *
  938. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  939. * devices that match the driver's criteria and unbinds.
  940. */
  941. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  942. {
  943. driver_unregister(&driver->drv);
  944. }
  945. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  946. /* --------------------------------------------------------------------------
  947. Device Enumeration
  948. -------------------------------------------------------------------------- */
  949. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  950. {
  951. struct acpi_device *device = NULL;
  952. acpi_status status;
  953. /*
  954. * Fixed hardware devices do not appear in the namespace and do not
  955. * have handles, but we fabricate acpi_devices for them, so we have
  956. * to deal with them specially.
  957. */
  958. if (!handle)
  959. return acpi_root;
  960. do {
  961. status = acpi_get_parent(handle, &handle);
  962. if (ACPI_FAILURE(status))
  963. return status == AE_NULL_ENTRY ? NULL : acpi_root;
  964. } while (acpi_bus_get_device(handle, &device));
  965. return device;
  966. }
  967. acpi_status
  968. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  969. {
  970. acpi_status status;
  971. acpi_handle tmp;
  972. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  973. union acpi_object *obj;
  974. status = acpi_get_handle(handle, "_EJD", &tmp);
  975. if (ACPI_FAILURE(status))
  976. return status;
  977. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  978. if (ACPI_SUCCESS(status)) {
  979. obj = buffer.pointer;
  980. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  981. ejd);
  982. kfree(buffer.pointer);
  983. }
  984. return status;
  985. }
  986. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  987. static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  988. struct acpi_device_wakeup *wakeup)
  989. {
  990. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  991. union acpi_object *package = NULL;
  992. union acpi_object *element = NULL;
  993. acpi_status status;
  994. int err = -ENODATA;
  995. if (!wakeup)
  996. return -EINVAL;
  997. INIT_LIST_HEAD(&wakeup->resources);
  998. /* _PRW */
  999. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  1000. if (ACPI_FAILURE(status)) {
  1001. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  1002. return err;
  1003. }
  1004. package = (union acpi_object *)buffer.pointer;
  1005. if (!package || package->package.count < 2)
  1006. goto out;
  1007. element = &(package->package.elements[0]);
  1008. if (!element)
  1009. goto out;
  1010. if (element->type == ACPI_TYPE_PACKAGE) {
  1011. if ((element->package.count < 2) ||
  1012. (element->package.elements[0].type !=
  1013. ACPI_TYPE_LOCAL_REFERENCE)
  1014. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  1015. goto out;
  1016. wakeup->gpe_device =
  1017. element->package.elements[0].reference.handle;
  1018. wakeup->gpe_number =
  1019. (u32) element->package.elements[1].integer.value;
  1020. } else if (element->type == ACPI_TYPE_INTEGER) {
  1021. wakeup->gpe_device = NULL;
  1022. wakeup->gpe_number = element->integer.value;
  1023. } else {
  1024. goto out;
  1025. }
  1026. element = &(package->package.elements[1]);
  1027. if (element->type != ACPI_TYPE_INTEGER)
  1028. goto out;
  1029. wakeup->sleep_state = element->integer.value;
  1030. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  1031. if (err)
  1032. goto out;
  1033. if (!list_empty(&wakeup->resources)) {
  1034. int sleep_state;
  1035. err = acpi_power_wakeup_list_init(&wakeup->resources,
  1036. &sleep_state);
  1037. if (err) {
  1038. acpi_handle_warn(handle, "Retrieving current states "
  1039. "of wakeup power resources failed\n");
  1040. acpi_power_resources_list_free(&wakeup->resources);
  1041. goto out;
  1042. }
  1043. if (sleep_state < wakeup->sleep_state) {
  1044. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  1045. "(S%d) by S%d from power resources\n",
  1046. (int)wakeup->sleep_state, sleep_state);
  1047. wakeup->sleep_state = sleep_state;
  1048. }
  1049. }
  1050. acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
  1051. out:
  1052. kfree(buffer.pointer);
  1053. return err;
  1054. }
  1055. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  1056. {
  1057. struct acpi_device_id button_device_ids[] = {
  1058. {"PNP0C0C", 0},
  1059. {"PNP0C0D", 0},
  1060. {"PNP0C0E", 0},
  1061. {"", 0},
  1062. };
  1063. acpi_status status;
  1064. acpi_event_status event_status;
  1065. device->wakeup.flags.notifier_present = 0;
  1066. /* Power button, Lid switch always enable wakeup */
  1067. if (!acpi_match_device_ids(device, button_device_ids)) {
  1068. device->wakeup.flags.run_wake = 1;
  1069. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  1070. /* Do not use Lid/sleep button for S5 wakeup */
  1071. if (device->wakeup.sleep_state == ACPI_STATE_S5)
  1072. device->wakeup.sleep_state = ACPI_STATE_S4;
  1073. }
  1074. device_set_wakeup_capable(&device->dev, true);
  1075. return;
  1076. }
  1077. status = acpi_get_gpe_status(device->wakeup.gpe_device,
  1078. device->wakeup.gpe_number,
  1079. &event_status);
  1080. if (status == AE_OK)
  1081. device->wakeup.flags.run_wake =
  1082. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  1083. }
  1084. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  1085. {
  1086. int err;
  1087. /* Presence of _PRW indicates wake capable */
  1088. if (!acpi_has_method(device->handle, "_PRW"))
  1089. return;
  1090. err = acpi_bus_extract_wakeup_device_power_package(device->handle,
  1091. &device->wakeup);
  1092. if (err) {
  1093. dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
  1094. return;
  1095. }
  1096. device->wakeup.flags.valid = 1;
  1097. device->wakeup.prepare_count = 0;
  1098. acpi_bus_set_run_wake_flags(device);
  1099. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  1100. * system for the ACPI device with the _PRW object.
  1101. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  1102. * So it is necessary to call _DSW object first. Only when it is not
  1103. * present will the _PSW object used.
  1104. */
  1105. err = acpi_device_sleep_wake(device, 0, 0, 0);
  1106. if (err)
  1107. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1108. "error in _DSW or _PSW evaluation\n"));
  1109. }
  1110. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  1111. {
  1112. struct acpi_device_power_state *ps = &device->power.states[state];
  1113. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  1114. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1115. acpi_status status;
  1116. INIT_LIST_HEAD(&ps->resources);
  1117. /* Evaluate "_PRx" to get referenced power resources */
  1118. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  1119. if (ACPI_SUCCESS(status)) {
  1120. union acpi_object *package = buffer.pointer;
  1121. if (buffer.length && package
  1122. && package->type == ACPI_TYPE_PACKAGE
  1123. && package->package.count) {
  1124. int err = acpi_extract_power_resources(package, 0,
  1125. &ps->resources);
  1126. if (!err)
  1127. device->power.flags.power_resources = 1;
  1128. }
  1129. ACPI_FREE(buffer.pointer);
  1130. }
  1131. /* Evaluate "_PSx" to see if we can do explicit sets */
  1132. pathname[2] = 'S';
  1133. if (acpi_has_method(device->handle, pathname))
  1134. ps->flags.explicit_set = 1;
  1135. /*
  1136. * State is valid if there are means to put the device into it.
  1137. * D3hot is only valid if _PR3 present.
  1138. */
  1139. if (!list_empty(&ps->resources)
  1140. || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
  1141. ps->flags.valid = 1;
  1142. ps->flags.os_accessible = 1;
  1143. }
  1144. ps->power = -1; /* Unknown - driver assigned */
  1145. ps->latency = -1; /* Unknown - driver assigned */
  1146. }
  1147. static void acpi_bus_get_power_flags(struct acpi_device *device)
  1148. {
  1149. u32 i;
  1150. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  1151. if (!acpi_has_method(device->handle, "_PS0") &&
  1152. !acpi_has_method(device->handle, "_PR0"))
  1153. return;
  1154. device->flags.power_manageable = 1;
  1155. /*
  1156. * Power Management Flags
  1157. */
  1158. if (acpi_has_method(device->handle, "_PSC"))
  1159. device->power.flags.explicit_get = 1;
  1160. if (acpi_has_method(device->handle, "_IRC"))
  1161. device->power.flags.inrush_current = 1;
  1162. /*
  1163. * Enumerate supported power management states
  1164. */
  1165. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  1166. acpi_bus_init_power_state(device, i);
  1167. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  1168. /* Set defaults for D0 and D3 states (always valid) */
  1169. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  1170. device->power.states[ACPI_STATE_D0].power = 100;
  1171. device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
  1172. device->power.states[ACPI_STATE_D3_COLD].power = 0;
  1173. /* Set D3cold's explicit_set flag if _PS3 exists. */
  1174. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
  1175. device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
  1176. /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
  1177. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
  1178. device->power.flags.power_resources)
  1179. device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
  1180. if (acpi_bus_init_power(device)) {
  1181. acpi_free_power_resources_lists(device);
  1182. device->flags.power_manageable = 0;
  1183. }
  1184. }
  1185. static void acpi_bus_get_flags(struct acpi_device *device)
  1186. {
  1187. /* Presence of _STA indicates 'dynamic_status' */
  1188. if (acpi_has_method(device->handle, "_STA"))
  1189. device->flags.dynamic_status = 1;
  1190. /* Presence of _RMV indicates 'removable' */
  1191. if (acpi_has_method(device->handle, "_RMV"))
  1192. device->flags.removable = 1;
  1193. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  1194. if (acpi_has_method(device->handle, "_EJD") ||
  1195. acpi_has_method(device->handle, "_EJ0"))
  1196. device->flags.ejectable = 1;
  1197. }
  1198. static void acpi_device_get_busid(struct acpi_device *device)
  1199. {
  1200. char bus_id[5] = { '?', 0 };
  1201. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  1202. int i = 0;
  1203. /*
  1204. * Bus ID
  1205. * ------
  1206. * The device's Bus ID is simply the object name.
  1207. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  1208. */
  1209. if (ACPI_IS_ROOT_DEVICE(device)) {
  1210. strcpy(device->pnp.bus_id, "ACPI");
  1211. return;
  1212. }
  1213. switch (device->device_type) {
  1214. case ACPI_BUS_TYPE_POWER_BUTTON:
  1215. strcpy(device->pnp.bus_id, "PWRF");
  1216. break;
  1217. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1218. strcpy(device->pnp.bus_id, "SLPF");
  1219. break;
  1220. default:
  1221. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  1222. /* Clean up trailing underscores (if any) */
  1223. for (i = 3; i > 1; i--) {
  1224. if (bus_id[i] == '_')
  1225. bus_id[i] = '\0';
  1226. else
  1227. break;
  1228. }
  1229. strcpy(device->pnp.bus_id, bus_id);
  1230. break;
  1231. }
  1232. }
  1233. /*
  1234. * acpi_ata_match - see if an acpi object is an ATA device
  1235. *
  1236. * If an acpi object has one of the ACPI ATA methods defined,
  1237. * then we can safely call it an ATA device.
  1238. */
  1239. bool acpi_ata_match(acpi_handle handle)
  1240. {
  1241. return acpi_has_method(handle, "_GTF") ||
  1242. acpi_has_method(handle, "_GTM") ||
  1243. acpi_has_method(handle, "_STM") ||
  1244. acpi_has_method(handle, "_SDD");
  1245. }
  1246. /*
  1247. * acpi_bay_match - see if an acpi object is an ejectable driver bay
  1248. *
  1249. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  1250. * then we can safely call it an ejectable drive bay
  1251. */
  1252. bool acpi_bay_match(acpi_handle handle)
  1253. {
  1254. acpi_handle phandle;
  1255. if (!acpi_has_method(handle, "_EJ0"))
  1256. return false;
  1257. if (acpi_ata_match(handle))
  1258. return true;
  1259. if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
  1260. return false;
  1261. return acpi_ata_match(phandle);
  1262. }
  1263. /*
  1264. * acpi_dock_match - see if an acpi object has a _DCK method
  1265. */
  1266. bool acpi_dock_match(acpi_handle handle)
  1267. {
  1268. return acpi_has_method(handle, "_DCK");
  1269. }
  1270. const char *acpi_device_hid(struct acpi_device *device)
  1271. {
  1272. struct acpi_hardware_id *hid;
  1273. if (list_empty(&device->pnp.ids))
  1274. return dummy_hid;
  1275. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1276. return hid->id;
  1277. }
  1278. EXPORT_SYMBOL(acpi_device_hid);
  1279. static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
  1280. {
  1281. struct acpi_hardware_id *id;
  1282. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1283. if (!id)
  1284. return;
  1285. id->id = kstrdup(dev_id, GFP_KERNEL);
  1286. if (!id->id) {
  1287. kfree(id);
  1288. return;
  1289. }
  1290. list_add_tail(&id->list, &pnp->ids);
  1291. pnp->type.hardware_id = 1;
  1292. }
  1293. /*
  1294. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1295. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1296. * prefix. Work around this.
  1297. */
  1298. static bool acpi_ibm_smbus_match(acpi_handle handle)
  1299. {
  1300. char node_name[ACPI_PATH_SEGMENT_LENGTH];
  1301. struct acpi_buffer path = { sizeof(node_name), node_name };
  1302. if (!dmi_name_in_vendors("IBM"))
  1303. return false;
  1304. /* Look for SMBS object */
  1305. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
  1306. strcmp("SMBS", path.pointer))
  1307. return false;
  1308. /* Does it have the necessary (but misnamed) methods? */
  1309. if (acpi_has_method(handle, "SBI") &&
  1310. acpi_has_method(handle, "SBR") &&
  1311. acpi_has_method(handle, "SBW"))
  1312. return true;
  1313. return false;
  1314. }
  1315. static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
  1316. int device_type)
  1317. {
  1318. acpi_status status;
  1319. struct acpi_device_info *info;
  1320. struct acpi_pnp_device_id_list *cid_list;
  1321. int i;
  1322. switch (device_type) {
  1323. case ACPI_BUS_TYPE_DEVICE:
  1324. if (handle == ACPI_ROOT_OBJECT) {
  1325. acpi_add_id(pnp, ACPI_SYSTEM_HID);
  1326. break;
  1327. }
  1328. status = acpi_get_object_info(handle, &info);
  1329. if (ACPI_FAILURE(status)) {
  1330. pr_err(PREFIX "%s: Error reading device info\n",
  1331. __func__);
  1332. return;
  1333. }
  1334. if (info->valid & ACPI_VALID_HID)
  1335. acpi_add_id(pnp, info->hardware_id.string);
  1336. if (info->valid & ACPI_VALID_CID) {
  1337. cid_list = &info->compatible_id_list;
  1338. for (i = 0; i < cid_list->count; i++)
  1339. acpi_add_id(pnp, cid_list->ids[i].string);
  1340. }
  1341. if (info->valid & ACPI_VALID_ADR) {
  1342. pnp->bus_address = info->address;
  1343. pnp->type.bus_address = 1;
  1344. }
  1345. if (info->valid & ACPI_VALID_UID)
  1346. pnp->unique_id = kstrdup(info->unique_id.string,
  1347. GFP_KERNEL);
  1348. kfree(info);
  1349. /*
  1350. * Some devices don't reliably have _HIDs & _CIDs, so add
  1351. * synthetic HIDs to make sure drivers can find them.
  1352. */
  1353. if (acpi_is_video_device(handle))
  1354. acpi_add_id(pnp, ACPI_VIDEO_HID);
  1355. else if (acpi_bay_match(handle))
  1356. acpi_add_id(pnp, ACPI_BAY_HID);
  1357. else if (acpi_dock_match(handle))
  1358. acpi_add_id(pnp, ACPI_DOCK_HID);
  1359. else if (acpi_ibm_smbus_match(handle))
  1360. acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
  1361. else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
  1362. acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
  1363. strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
  1364. strcpy(pnp->device_class, ACPI_BUS_CLASS);
  1365. }
  1366. break;
  1367. case ACPI_BUS_TYPE_POWER:
  1368. acpi_add_id(pnp, ACPI_POWER_HID);
  1369. break;
  1370. case ACPI_BUS_TYPE_PROCESSOR:
  1371. acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
  1372. break;
  1373. case ACPI_BUS_TYPE_THERMAL:
  1374. acpi_add_id(pnp, ACPI_THERMAL_HID);
  1375. break;
  1376. case ACPI_BUS_TYPE_POWER_BUTTON:
  1377. acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
  1378. break;
  1379. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1380. acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
  1381. break;
  1382. }
  1383. }
  1384. void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
  1385. {
  1386. struct acpi_hardware_id *id, *tmp;
  1387. list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
  1388. kfree(id->id);
  1389. kfree(id);
  1390. }
  1391. kfree(pnp->unique_id);
  1392. }
  1393. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1394. int type, unsigned long long sta)
  1395. {
  1396. INIT_LIST_HEAD(&device->pnp.ids);
  1397. device->device_type = type;
  1398. device->handle = handle;
  1399. device->parent = acpi_bus_get_parent(handle);
  1400. STRUCT_TO_INT(device->status) = sta;
  1401. acpi_device_get_busid(device);
  1402. acpi_set_pnp_ids(handle, &device->pnp, type);
  1403. acpi_bus_get_flags(device);
  1404. device->flags.match_driver = false;
  1405. device_initialize(&device->dev);
  1406. dev_set_uevent_suppress(&device->dev, true);
  1407. }
  1408. void acpi_device_add_finalize(struct acpi_device *device)
  1409. {
  1410. dev_set_uevent_suppress(&device->dev, false);
  1411. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1412. }
  1413. static int acpi_add_single_object(struct acpi_device **child,
  1414. acpi_handle handle, int type,
  1415. unsigned long long sta)
  1416. {
  1417. int result;
  1418. struct acpi_device *device;
  1419. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1420. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1421. if (!device) {
  1422. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1423. return -ENOMEM;
  1424. }
  1425. acpi_init_device_object(device, handle, type, sta);
  1426. acpi_bus_get_power_flags(device);
  1427. acpi_bus_get_wakeup_device_flags(device);
  1428. result = acpi_device_add(device, acpi_device_release);
  1429. if (result) {
  1430. acpi_device_release(&device->dev);
  1431. return result;
  1432. }
  1433. acpi_power_add_remove_device(device, true);
  1434. acpi_device_add_finalize(device);
  1435. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1436. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1437. dev_name(&device->dev), (char *) buffer.pointer,
  1438. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1439. kfree(buffer.pointer);
  1440. *child = device;
  1441. return 0;
  1442. }
  1443. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1444. unsigned long long *sta)
  1445. {
  1446. acpi_status status;
  1447. acpi_object_type acpi_type;
  1448. status = acpi_get_type(handle, &acpi_type);
  1449. if (ACPI_FAILURE(status))
  1450. return -ENODEV;
  1451. switch (acpi_type) {
  1452. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1453. case ACPI_TYPE_DEVICE:
  1454. *type = ACPI_BUS_TYPE_DEVICE;
  1455. status = acpi_bus_get_status_handle(handle, sta);
  1456. if (ACPI_FAILURE(status))
  1457. return -ENODEV;
  1458. break;
  1459. case ACPI_TYPE_PROCESSOR:
  1460. *type = ACPI_BUS_TYPE_PROCESSOR;
  1461. status = acpi_bus_get_status_handle(handle, sta);
  1462. if (ACPI_FAILURE(status))
  1463. return -ENODEV;
  1464. break;
  1465. case ACPI_TYPE_THERMAL:
  1466. *type = ACPI_BUS_TYPE_THERMAL;
  1467. *sta = ACPI_STA_DEFAULT;
  1468. break;
  1469. case ACPI_TYPE_POWER:
  1470. *type = ACPI_BUS_TYPE_POWER;
  1471. *sta = ACPI_STA_DEFAULT;
  1472. break;
  1473. default:
  1474. return -ENODEV;
  1475. }
  1476. return 0;
  1477. }
  1478. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1479. char *idstr,
  1480. const struct acpi_device_id **matchid)
  1481. {
  1482. const struct acpi_device_id *devid;
  1483. for (devid = handler->ids; devid->id[0]; devid++)
  1484. if (!strcmp((char *)devid->id, idstr)) {
  1485. if (matchid)
  1486. *matchid = devid;
  1487. return true;
  1488. }
  1489. return false;
  1490. }
  1491. static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
  1492. const struct acpi_device_id **matchid)
  1493. {
  1494. struct acpi_scan_handler *handler;
  1495. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1496. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1497. return handler;
  1498. return NULL;
  1499. }
  1500. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1501. {
  1502. if (!!hotplug->enabled == !!val)
  1503. return;
  1504. mutex_lock(&acpi_scan_lock);
  1505. hotplug->enabled = val;
  1506. mutex_unlock(&acpi_scan_lock);
  1507. }
  1508. static void acpi_scan_init_hotplug(acpi_handle handle, int type)
  1509. {
  1510. struct acpi_device_pnp pnp = {};
  1511. struct acpi_hardware_id *hwid;
  1512. struct acpi_scan_handler *handler;
  1513. INIT_LIST_HEAD(&pnp.ids);
  1514. acpi_set_pnp_ids(handle, &pnp, type);
  1515. if (!pnp.type.hardware_id)
  1516. goto out;
  1517. /*
  1518. * This relies on the fact that acpi_install_notify_handler() will not
  1519. * install the same notify handler routine twice for the same handle.
  1520. */
  1521. list_for_each_entry(hwid, &pnp.ids, list) {
  1522. handler = acpi_scan_match_handler(hwid->id, NULL);
  1523. if (handler) {
  1524. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1525. acpi_hotplug_notify_cb, handler);
  1526. break;
  1527. }
  1528. }
  1529. out:
  1530. acpi_free_pnp_ids(&pnp);
  1531. }
  1532. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1533. void *not_used, void **return_value)
  1534. {
  1535. struct acpi_device *device = NULL;
  1536. int type;
  1537. unsigned long long sta;
  1538. int result;
  1539. acpi_bus_get_device(handle, &device);
  1540. if (device)
  1541. goto out;
  1542. result = acpi_bus_type_and_status(handle, &type, &sta);
  1543. if (result)
  1544. return AE_OK;
  1545. if (type == ACPI_BUS_TYPE_POWER) {
  1546. acpi_add_power_resource(handle);
  1547. return AE_OK;
  1548. }
  1549. acpi_scan_init_hotplug(handle, type);
  1550. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1551. !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
  1552. struct acpi_device_wakeup wakeup;
  1553. if (acpi_has_method(handle, "_PRW")) {
  1554. acpi_bus_extract_wakeup_device_power_package(handle,
  1555. &wakeup);
  1556. acpi_power_resources_list_free(&wakeup.resources);
  1557. }
  1558. return AE_CTRL_DEPTH;
  1559. }
  1560. acpi_add_single_object(&device, handle, type, sta);
  1561. if (!device)
  1562. return AE_CTRL_DEPTH;
  1563. out:
  1564. if (!*return_value)
  1565. *return_value = device;
  1566. return AE_OK;
  1567. }
  1568. static int acpi_scan_attach_handler(struct acpi_device *device)
  1569. {
  1570. struct acpi_hardware_id *hwid;
  1571. int ret = 0;
  1572. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1573. const struct acpi_device_id *devid;
  1574. struct acpi_scan_handler *handler;
  1575. handler = acpi_scan_match_handler(hwid->id, &devid);
  1576. if (handler) {
  1577. ret = handler->attach(device, devid);
  1578. if (ret > 0) {
  1579. device->handler = handler;
  1580. break;
  1581. } else if (ret < 0) {
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. return ret;
  1587. }
  1588. static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
  1589. void *not_used, void **ret_not_used)
  1590. {
  1591. struct acpi_device *device;
  1592. unsigned long long sta_not_used;
  1593. int ret;
  1594. /*
  1595. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1596. * namespace walks prematurely.
  1597. */
  1598. if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
  1599. return AE_OK;
  1600. if (acpi_bus_get_device(handle, &device))
  1601. return AE_CTRL_DEPTH;
  1602. if (device->handler)
  1603. return AE_OK;
  1604. ret = acpi_scan_attach_handler(device);
  1605. if (ret < 0)
  1606. return AE_CTRL_DEPTH;
  1607. device->flags.match_driver = true;
  1608. if (ret > 0)
  1609. return AE_OK;
  1610. ret = device_attach(&device->dev);
  1611. return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
  1612. }
  1613. /**
  1614. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1615. * @handle: Root of the namespace scope to scan.
  1616. *
  1617. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1618. * found devices.
  1619. *
  1620. * If no devices were found, -ENODEV is returned, but it does not mean that
  1621. * there has been a real error. There just have been no suitable ACPI objects
  1622. * in the table trunk from which the kernel could create a device and add an
  1623. * appropriate driver.
  1624. *
  1625. * Must be called under acpi_scan_lock.
  1626. */
  1627. int acpi_bus_scan(acpi_handle handle)
  1628. {
  1629. void *device = NULL;
  1630. int error = 0;
  1631. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1632. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1633. acpi_bus_check_add, NULL, NULL, &device);
  1634. if (!device)
  1635. error = -ENODEV;
  1636. else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
  1637. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1638. acpi_bus_device_attach, NULL, NULL, NULL);
  1639. return error;
  1640. }
  1641. EXPORT_SYMBOL(acpi_bus_scan);
  1642. static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
  1643. void *not_used, void **ret_not_used)
  1644. {
  1645. struct acpi_device *device = NULL;
  1646. if (!acpi_bus_get_device(handle, &device)) {
  1647. struct acpi_scan_handler *dev_handler = device->handler;
  1648. if (dev_handler) {
  1649. if (dev_handler->detach)
  1650. dev_handler->detach(device);
  1651. device->handler = NULL;
  1652. } else {
  1653. device_release_driver(&device->dev);
  1654. }
  1655. }
  1656. return AE_OK;
  1657. }
  1658. static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
  1659. void *not_used, void **ret_not_used)
  1660. {
  1661. struct acpi_device *device = NULL;
  1662. if (!acpi_bus_get_device(handle, &device))
  1663. acpi_device_unregister(device);
  1664. return AE_OK;
  1665. }
  1666. /**
  1667. * acpi_bus_trim - Remove ACPI device node and all of its descendants
  1668. * @start: Root of the ACPI device nodes subtree to remove.
  1669. *
  1670. * Must be called under acpi_scan_lock.
  1671. */
  1672. void acpi_bus_trim(struct acpi_device *start)
  1673. {
  1674. /*
  1675. * Execute acpi_bus_device_detach() as a post-order callback to detach
  1676. * all ACPI drivers from the device nodes being removed.
  1677. */
  1678. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1679. acpi_bus_device_detach, NULL, NULL);
  1680. acpi_bus_device_detach(start->handle, 0, NULL, NULL);
  1681. /*
  1682. * Execute acpi_bus_remove() as a post-order callback to remove device
  1683. * nodes in the given namespace scope.
  1684. */
  1685. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1686. acpi_bus_remove, NULL, NULL);
  1687. acpi_bus_remove(start->handle, 0, NULL, NULL);
  1688. }
  1689. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1690. static int acpi_bus_scan_fixed(void)
  1691. {
  1692. int result = 0;
  1693. /*
  1694. * Enumerate all fixed-feature devices.
  1695. */
  1696. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1697. struct acpi_device *device = NULL;
  1698. result = acpi_add_single_object(&device, NULL,
  1699. ACPI_BUS_TYPE_POWER_BUTTON,
  1700. ACPI_STA_DEFAULT);
  1701. if (result)
  1702. return result;
  1703. result = device_attach(&device->dev);
  1704. if (result < 0)
  1705. return result;
  1706. device_init_wakeup(&device->dev, true);
  1707. }
  1708. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1709. struct acpi_device *device = NULL;
  1710. result = acpi_add_single_object(&device, NULL,
  1711. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1712. ACPI_STA_DEFAULT);
  1713. if (result)
  1714. return result;
  1715. result = device_attach(&device->dev);
  1716. }
  1717. return result < 0 ? result : 0;
  1718. }
  1719. int __init acpi_scan_init(void)
  1720. {
  1721. int result;
  1722. result = bus_register(&acpi_bus_type);
  1723. if (result) {
  1724. /* We don't want to quit even if we failed to add suspend/resume */
  1725. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1726. }
  1727. acpi_pci_root_init();
  1728. acpi_pci_link_init();
  1729. acpi_processor_init();
  1730. acpi_platform_init();
  1731. acpi_lpss_init();
  1732. acpi_cmos_rtc_init();
  1733. acpi_container_init();
  1734. acpi_memory_hotplug_init();
  1735. acpi_dock_init();
  1736. mutex_lock(&acpi_scan_lock);
  1737. /*
  1738. * Enumerate devices in the ACPI namespace.
  1739. */
  1740. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1741. if (result)
  1742. goto out;
  1743. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1744. if (result)
  1745. goto out;
  1746. result = acpi_bus_scan_fixed();
  1747. if (result) {
  1748. acpi_device_unregister(acpi_root);
  1749. goto out;
  1750. }
  1751. acpi_update_all_gpes();
  1752. acpi_pci_root_hp_init();
  1753. out:
  1754. mutex_unlock(&acpi_scan_lock);
  1755. return result;
  1756. }