scan.c 56 KB

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