scan.c 55 KB

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