scan.c 53 KB

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