scan.c 54 KB

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