scan.c 52 KB

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