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