scan.c 53 KB

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