scan.c 54 KB

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