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