scan.c 55 KB

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