scan.c 52 KB

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