scan.c 53 KB

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