scan.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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_device_probe(struct device *dev)
  689. {
  690. struct acpi_device *acpi_dev = to_acpi_device(dev);
  691. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  692. int ret;
  693. if (!acpi_drv->ops.add)
  694. return -ENOSYS;
  695. ret = acpi_drv->ops.add(acpi_dev);
  696. if (ret)
  697. return ret;
  698. acpi_dev->driver = acpi_drv;
  699. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  700. "Driver [%s] successfully bound to device [%s]\n",
  701. acpi_drv->name, acpi_dev->pnp.bus_id));
  702. if (acpi_drv->ops.notify) {
  703. ret = acpi_device_install_notify_handler(acpi_dev);
  704. if (ret) {
  705. if (acpi_drv->ops.remove)
  706. acpi_drv->ops.remove(acpi_dev);
  707. acpi_dev->driver = NULL;
  708. acpi_dev->driver_data = NULL;
  709. return ret;
  710. }
  711. }
  712. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
  713. acpi_drv->name, acpi_dev->pnp.bus_id));
  714. get_device(dev);
  715. return 0;
  716. }
  717. static int acpi_device_remove(struct device * dev)
  718. {
  719. struct acpi_device *acpi_dev = to_acpi_device(dev);
  720. struct acpi_driver *acpi_drv = acpi_dev->driver;
  721. if (acpi_drv) {
  722. if (acpi_drv->ops.notify)
  723. acpi_device_remove_notify_handler(acpi_dev);
  724. if (acpi_drv->ops.remove)
  725. acpi_drv->ops.remove(acpi_dev);
  726. }
  727. acpi_dev->driver = NULL;
  728. acpi_dev->driver_data = NULL;
  729. put_device(dev);
  730. return 0;
  731. }
  732. struct bus_type acpi_bus_type = {
  733. .name = "acpi",
  734. .match = acpi_bus_match,
  735. .probe = acpi_device_probe,
  736. .remove = acpi_device_remove,
  737. .uevent = acpi_device_uevent,
  738. };
  739. int acpi_device_add(struct acpi_device *device,
  740. void (*release)(struct device *))
  741. {
  742. int result;
  743. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  744. int found = 0;
  745. if (device->handle) {
  746. acpi_status status;
  747. status = acpi_attach_data(device->handle, acpi_bus_data_handler,
  748. device);
  749. if (ACPI_FAILURE(status)) {
  750. acpi_handle_err(device->handle,
  751. "Unable to attach device data\n");
  752. return -ENODEV;
  753. }
  754. }
  755. /*
  756. * Linkage
  757. * -------
  758. * Link this device to its parent and siblings.
  759. */
  760. INIT_LIST_HEAD(&device->children);
  761. INIT_LIST_HEAD(&device->node);
  762. INIT_LIST_HEAD(&device->wakeup_list);
  763. INIT_LIST_HEAD(&device->physical_node_list);
  764. mutex_init(&device->physical_node_lock);
  765. INIT_LIST_HEAD(&device->power_dependent);
  766. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  767. if (!new_bus_id) {
  768. pr_err(PREFIX "Memory allocation error\n");
  769. result = -ENOMEM;
  770. goto err_detach;
  771. }
  772. mutex_lock(&acpi_device_lock);
  773. /*
  774. * Find suitable bus_id and instance number in acpi_bus_id_list
  775. * If failed, create one and link it into acpi_bus_id_list
  776. */
  777. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  778. if (!strcmp(acpi_device_bus_id->bus_id,
  779. acpi_device_hid(device))) {
  780. acpi_device_bus_id->instance_no++;
  781. found = 1;
  782. kfree(new_bus_id);
  783. break;
  784. }
  785. }
  786. if (!found) {
  787. acpi_device_bus_id = new_bus_id;
  788. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  789. acpi_device_bus_id->instance_no = 0;
  790. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  791. }
  792. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  793. if (device->parent)
  794. list_add_tail(&device->node, &device->parent->children);
  795. if (device->wakeup.flags.valid)
  796. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  797. mutex_unlock(&acpi_device_lock);
  798. if (device->parent)
  799. device->dev.parent = &device->parent->dev;
  800. device->dev.bus = &acpi_bus_type;
  801. device->dev.release = release;
  802. result = device_add(&device->dev);
  803. if (result) {
  804. dev_err(&device->dev, "Error registering device\n");
  805. goto err;
  806. }
  807. result = acpi_device_setup_files(device);
  808. if (result)
  809. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  810. dev_name(&device->dev));
  811. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  812. return 0;
  813. err:
  814. mutex_lock(&acpi_device_lock);
  815. if (device->parent)
  816. list_del(&device->node);
  817. list_del(&device->wakeup_list);
  818. mutex_unlock(&acpi_device_lock);
  819. err_detach:
  820. acpi_detach_data(device->handle, acpi_bus_data_handler);
  821. return result;
  822. }
  823. static void acpi_device_unregister(struct acpi_device *device)
  824. {
  825. mutex_lock(&acpi_device_lock);
  826. if (device->parent)
  827. list_del(&device->node);
  828. list_del(&device->wakeup_list);
  829. mutex_unlock(&acpi_device_lock);
  830. acpi_detach_data(device->handle, acpi_bus_data_handler);
  831. acpi_power_add_remove_device(device, false);
  832. acpi_device_remove_files(device);
  833. if (device->remove)
  834. device->remove(device);
  835. device_del(&device->dev);
  836. /*
  837. * Transition the device to D3cold to drop the reference counts of all
  838. * power resources the device depends on and turn off the ones that have
  839. * no more references.
  840. */
  841. acpi_device_set_power(device, ACPI_STATE_D3_COLD);
  842. device->handle = NULL;
  843. put_device(&device->dev);
  844. }
  845. /* --------------------------------------------------------------------------
  846. Driver Management
  847. -------------------------------------------------------------------------- */
  848. /**
  849. * acpi_bus_register_driver - register a driver with the ACPI bus
  850. * @driver: driver being registered
  851. *
  852. * Registers a driver with the ACPI bus. Searches the namespace for all
  853. * devices that match the driver's criteria and binds. Returns zero for
  854. * success or a negative error status for failure.
  855. */
  856. int acpi_bus_register_driver(struct acpi_driver *driver)
  857. {
  858. int ret;
  859. if (acpi_disabled)
  860. return -ENODEV;
  861. driver->drv.name = driver->name;
  862. driver->drv.bus = &acpi_bus_type;
  863. driver->drv.owner = driver->owner;
  864. ret = driver_register(&driver->drv);
  865. return ret;
  866. }
  867. EXPORT_SYMBOL(acpi_bus_register_driver);
  868. /**
  869. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  870. * @driver: driver to unregister
  871. *
  872. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  873. * devices that match the driver's criteria and unbinds.
  874. */
  875. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  876. {
  877. driver_unregister(&driver->drv);
  878. }
  879. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  880. /* --------------------------------------------------------------------------
  881. Device Enumeration
  882. -------------------------------------------------------------------------- */
  883. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  884. {
  885. struct acpi_device *device = NULL;
  886. acpi_status status;
  887. /*
  888. * Fixed hardware devices do not appear in the namespace and do not
  889. * have handles, but we fabricate acpi_devices for them, so we have
  890. * to deal with them specially.
  891. */
  892. if (!handle)
  893. return acpi_root;
  894. do {
  895. status = acpi_get_parent(handle, &handle);
  896. if (ACPI_FAILURE(status))
  897. return status == AE_NULL_ENTRY ? NULL : acpi_root;
  898. } while (acpi_bus_get_device(handle, &device));
  899. return device;
  900. }
  901. acpi_status
  902. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  903. {
  904. acpi_status status;
  905. acpi_handle tmp;
  906. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  907. union acpi_object *obj;
  908. status = acpi_get_handle(handle, "_EJD", &tmp);
  909. if (ACPI_FAILURE(status))
  910. return status;
  911. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  912. if (ACPI_SUCCESS(status)) {
  913. obj = buffer.pointer;
  914. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  915. ejd);
  916. kfree(buffer.pointer);
  917. }
  918. return status;
  919. }
  920. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  921. void acpi_bus_data_handler(acpi_handle handle, void *context)
  922. {
  923. /* TBD */
  924. return;
  925. }
  926. static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  927. struct acpi_device_wakeup *wakeup)
  928. {
  929. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  930. union acpi_object *package = NULL;
  931. union acpi_object *element = NULL;
  932. acpi_status status;
  933. int err = -ENODATA;
  934. if (!wakeup)
  935. return -EINVAL;
  936. INIT_LIST_HEAD(&wakeup->resources);
  937. /* _PRW */
  938. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  939. if (ACPI_FAILURE(status)) {
  940. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  941. return err;
  942. }
  943. package = (union acpi_object *)buffer.pointer;
  944. if (!package || package->package.count < 2)
  945. goto out;
  946. element = &(package->package.elements[0]);
  947. if (!element)
  948. goto out;
  949. if (element->type == ACPI_TYPE_PACKAGE) {
  950. if ((element->package.count < 2) ||
  951. (element->package.elements[0].type !=
  952. ACPI_TYPE_LOCAL_REFERENCE)
  953. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  954. goto out;
  955. wakeup->gpe_device =
  956. element->package.elements[0].reference.handle;
  957. wakeup->gpe_number =
  958. (u32) element->package.elements[1].integer.value;
  959. } else if (element->type == ACPI_TYPE_INTEGER) {
  960. wakeup->gpe_device = NULL;
  961. wakeup->gpe_number = element->integer.value;
  962. } else {
  963. goto out;
  964. }
  965. element = &(package->package.elements[1]);
  966. if (element->type != ACPI_TYPE_INTEGER)
  967. goto out;
  968. wakeup->sleep_state = element->integer.value;
  969. err = acpi_extract_power_resources(package, 2, &wakeup->resources);
  970. if (err)
  971. goto out;
  972. if (!list_empty(&wakeup->resources)) {
  973. int sleep_state;
  974. err = acpi_power_wakeup_list_init(&wakeup->resources,
  975. &sleep_state);
  976. if (err) {
  977. acpi_handle_warn(handle, "Retrieving current states "
  978. "of wakeup power resources failed\n");
  979. acpi_power_resources_list_free(&wakeup->resources);
  980. goto out;
  981. }
  982. if (sleep_state < wakeup->sleep_state) {
  983. acpi_handle_warn(handle, "Overriding _PRW sleep state "
  984. "(S%d) by S%d from power resources\n",
  985. (int)wakeup->sleep_state, sleep_state);
  986. wakeup->sleep_state = sleep_state;
  987. }
  988. }
  989. acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
  990. out:
  991. kfree(buffer.pointer);
  992. return err;
  993. }
  994. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  995. {
  996. struct acpi_device_id button_device_ids[] = {
  997. {"PNP0C0C", 0},
  998. {"PNP0C0D", 0},
  999. {"PNP0C0E", 0},
  1000. {"", 0},
  1001. };
  1002. acpi_status status;
  1003. acpi_event_status event_status;
  1004. device->wakeup.flags.notifier_present = 0;
  1005. /* Power button, Lid switch always enable wakeup */
  1006. if (!acpi_match_device_ids(device, button_device_ids)) {
  1007. device->wakeup.flags.run_wake = 1;
  1008. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  1009. /* Do not use Lid/sleep button for S5 wakeup */
  1010. if (device->wakeup.sleep_state == ACPI_STATE_S5)
  1011. device->wakeup.sleep_state = ACPI_STATE_S4;
  1012. }
  1013. device_set_wakeup_capable(&device->dev, true);
  1014. return;
  1015. }
  1016. status = acpi_get_gpe_status(device->wakeup.gpe_device,
  1017. device->wakeup.gpe_number,
  1018. &event_status);
  1019. if (status == AE_OK)
  1020. device->wakeup.flags.run_wake =
  1021. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  1022. }
  1023. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  1024. {
  1025. acpi_handle temp;
  1026. acpi_status status = 0;
  1027. int err;
  1028. /* Presence of _PRW indicates wake capable */
  1029. status = acpi_get_handle(device->handle, "_PRW", &temp);
  1030. if (ACPI_FAILURE(status))
  1031. return;
  1032. err = acpi_bus_extract_wakeup_device_power_package(device->handle,
  1033. &device->wakeup);
  1034. if (err) {
  1035. dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
  1036. return;
  1037. }
  1038. device->wakeup.flags.valid = 1;
  1039. device->wakeup.prepare_count = 0;
  1040. acpi_bus_set_run_wake_flags(device);
  1041. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  1042. * system for the ACPI device with the _PRW object.
  1043. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  1044. * So it is necessary to call _DSW object first. Only when it is not
  1045. * present will the _PSW object used.
  1046. */
  1047. err = acpi_device_sleep_wake(device, 0, 0, 0);
  1048. if (err)
  1049. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1050. "error in _DSW or _PSW evaluation\n"));
  1051. }
  1052. static void acpi_bus_init_power_state(struct acpi_device *device, int state)
  1053. {
  1054. struct acpi_device_power_state *ps = &device->power.states[state];
  1055. char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
  1056. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1057. acpi_handle handle;
  1058. acpi_status status;
  1059. INIT_LIST_HEAD(&ps->resources);
  1060. /* Evaluate "_PRx" to get referenced power resources */
  1061. status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
  1062. if (ACPI_SUCCESS(status)) {
  1063. union acpi_object *package = buffer.pointer;
  1064. if (buffer.length && package
  1065. && package->type == ACPI_TYPE_PACKAGE
  1066. && package->package.count) {
  1067. int err = acpi_extract_power_resources(package, 0,
  1068. &ps->resources);
  1069. if (!err)
  1070. device->power.flags.power_resources = 1;
  1071. }
  1072. ACPI_FREE(buffer.pointer);
  1073. }
  1074. /* Evaluate "_PSx" to see if we can do explicit sets */
  1075. pathname[2] = 'S';
  1076. status = acpi_get_handle(device->handle, pathname, &handle);
  1077. if (ACPI_SUCCESS(status))
  1078. ps->flags.explicit_set = 1;
  1079. /*
  1080. * State is valid if there are means to put the device into it.
  1081. * D3hot is only valid if _PR3 present.
  1082. */
  1083. if (!list_empty(&ps->resources)
  1084. || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
  1085. ps->flags.valid = 1;
  1086. ps->flags.os_accessible = 1;
  1087. }
  1088. ps->power = -1; /* Unknown - driver assigned */
  1089. ps->latency = -1; /* Unknown - driver assigned */
  1090. }
  1091. static void acpi_bus_get_power_flags(struct acpi_device *device)
  1092. {
  1093. acpi_status status;
  1094. acpi_handle handle;
  1095. u32 i;
  1096. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  1097. status = acpi_get_handle(device->handle, "_PS0", &handle);
  1098. if (ACPI_FAILURE(status)) {
  1099. status = acpi_get_handle(device->handle, "_PR0", &handle);
  1100. if (ACPI_FAILURE(status))
  1101. return;
  1102. }
  1103. device->flags.power_manageable = 1;
  1104. /*
  1105. * Power Management Flags
  1106. */
  1107. status = acpi_get_handle(device->handle, "_PSC", &handle);
  1108. if (ACPI_SUCCESS(status))
  1109. device->power.flags.explicit_get = 1;
  1110. status = acpi_get_handle(device->handle, "_IRC", &handle);
  1111. if (ACPI_SUCCESS(status))
  1112. device->power.flags.inrush_current = 1;
  1113. /*
  1114. * Enumerate supported power management states
  1115. */
  1116. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
  1117. acpi_bus_init_power_state(device, i);
  1118. INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
  1119. /* Set defaults for D0 and D3 states (always valid) */
  1120. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  1121. device->power.states[ACPI_STATE_D0].power = 100;
  1122. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  1123. device->power.states[ACPI_STATE_D3].power = 0;
  1124. /* Set D3cold's explicit_set flag if _PS3 exists. */
  1125. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
  1126. device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
  1127. /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
  1128. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
  1129. device->power.flags.power_resources)
  1130. device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
  1131. if (acpi_bus_init_power(device)) {
  1132. acpi_free_power_resources_lists(device);
  1133. device->flags.power_manageable = 0;
  1134. }
  1135. }
  1136. static void acpi_bus_get_flags(struct acpi_device *device)
  1137. {
  1138. acpi_status status = AE_OK;
  1139. acpi_handle temp = NULL;
  1140. /* Presence of _STA indicates 'dynamic_status' */
  1141. status = acpi_get_handle(device->handle, "_STA", &temp);
  1142. if (ACPI_SUCCESS(status))
  1143. device->flags.dynamic_status = 1;
  1144. /* Presence of _RMV indicates 'removable' */
  1145. status = acpi_get_handle(device->handle, "_RMV", &temp);
  1146. if (ACPI_SUCCESS(status))
  1147. device->flags.removable = 1;
  1148. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  1149. status = acpi_get_handle(device->handle, "_EJD", &temp);
  1150. if (ACPI_SUCCESS(status))
  1151. device->flags.ejectable = 1;
  1152. else {
  1153. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  1154. if (ACPI_SUCCESS(status))
  1155. device->flags.ejectable = 1;
  1156. }
  1157. }
  1158. static void acpi_device_get_busid(struct acpi_device *device)
  1159. {
  1160. char bus_id[5] = { '?', 0 };
  1161. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  1162. int i = 0;
  1163. /*
  1164. * Bus ID
  1165. * ------
  1166. * The device's Bus ID is simply the object name.
  1167. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  1168. */
  1169. if (ACPI_IS_ROOT_DEVICE(device)) {
  1170. strcpy(device->pnp.bus_id, "ACPI");
  1171. return;
  1172. }
  1173. switch (device->device_type) {
  1174. case ACPI_BUS_TYPE_POWER_BUTTON:
  1175. strcpy(device->pnp.bus_id, "PWRF");
  1176. break;
  1177. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1178. strcpy(device->pnp.bus_id, "SLPF");
  1179. break;
  1180. default:
  1181. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  1182. /* Clean up trailing underscores (if any) */
  1183. for (i = 3; i > 1; i--) {
  1184. if (bus_id[i] == '_')
  1185. bus_id[i] = '\0';
  1186. else
  1187. break;
  1188. }
  1189. strcpy(device->pnp.bus_id, bus_id);
  1190. break;
  1191. }
  1192. }
  1193. /*
  1194. * acpi_bay_match - see if an acpi object is an ejectable driver bay
  1195. *
  1196. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  1197. * then we can safely call it an ejectable drive bay
  1198. */
  1199. static int acpi_bay_match(acpi_handle handle)
  1200. {
  1201. acpi_status status;
  1202. acpi_handle tmp;
  1203. acpi_handle phandle;
  1204. status = acpi_get_handle(handle, "_EJ0", &tmp);
  1205. if (ACPI_FAILURE(status))
  1206. return -ENODEV;
  1207. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  1208. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  1209. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  1210. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  1211. return 0;
  1212. if (acpi_get_parent(handle, &phandle))
  1213. return -ENODEV;
  1214. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  1215. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  1216. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  1217. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  1218. return 0;
  1219. return -ENODEV;
  1220. }
  1221. /*
  1222. * acpi_dock_match - see if an acpi object has a _DCK method
  1223. */
  1224. static int acpi_dock_match(acpi_handle handle)
  1225. {
  1226. acpi_handle tmp;
  1227. return acpi_get_handle(handle, "_DCK", &tmp);
  1228. }
  1229. const char *acpi_device_hid(struct acpi_device *device)
  1230. {
  1231. struct acpi_hardware_id *hid;
  1232. if (list_empty(&device->pnp.ids))
  1233. return dummy_hid;
  1234. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1235. return hid->id;
  1236. }
  1237. EXPORT_SYMBOL(acpi_device_hid);
  1238. static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
  1239. {
  1240. struct acpi_hardware_id *id;
  1241. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1242. if (!id)
  1243. return;
  1244. id->id = kstrdup(dev_id, GFP_KERNEL);
  1245. if (!id->id) {
  1246. kfree(id);
  1247. return;
  1248. }
  1249. list_add_tail(&id->list, &pnp->ids);
  1250. pnp->type.hardware_id = 1;
  1251. }
  1252. /*
  1253. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1254. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1255. * prefix. Work around this.
  1256. */
  1257. static int acpi_ibm_smbus_match(acpi_handle handle)
  1258. {
  1259. acpi_handle h_dummy;
  1260. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  1261. int result;
  1262. if (!dmi_name_in_vendors("IBM"))
  1263. return -ENODEV;
  1264. /* Look for SMBS object */
  1265. result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
  1266. if (result)
  1267. return result;
  1268. if (strcmp("SMBS", path.pointer)) {
  1269. result = -ENODEV;
  1270. goto out;
  1271. }
  1272. /* Does it have the necessary (but misnamed) methods? */
  1273. result = -ENODEV;
  1274. if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
  1275. ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
  1276. ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
  1277. result = 0;
  1278. out:
  1279. kfree(path.pointer);
  1280. return result;
  1281. }
  1282. static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
  1283. int device_type)
  1284. {
  1285. acpi_status status;
  1286. struct acpi_device_info *info;
  1287. struct acpi_pnp_device_id_list *cid_list;
  1288. int i;
  1289. switch (device_type) {
  1290. case ACPI_BUS_TYPE_DEVICE:
  1291. if (handle == ACPI_ROOT_OBJECT) {
  1292. acpi_add_id(pnp, ACPI_SYSTEM_HID);
  1293. break;
  1294. }
  1295. status = acpi_get_object_info(handle, &info);
  1296. if (ACPI_FAILURE(status)) {
  1297. pr_err(PREFIX "%s: Error reading device info\n",
  1298. __func__);
  1299. return;
  1300. }
  1301. if (info->valid & ACPI_VALID_HID)
  1302. acpi_add_id(pnp, info->hardware_id.string);
  1303. if (info->valid & ACPI_VALID_CID) {
  1304. cid_list = &info->compatible_id_list;
  1305. for (i = 0; i < cid_list->count; i++)
  1306. acpi_add_id(pnp, cid_list->ids[i].string);
  1307. }
  1308. if (info->valid & ACPI_VALID_ADR) {
  1309. pnp->bus_address = info->address;
  1310. pnp->type.bus_address = 1;
  1311. }
  1312. if (info->valid & ACPI_VALID_UID)
  1313. pnp->unique_id = kstrdup(info->unique_id.string,
  1314. GFP_KERNEL);
  1315. kfree(info);
  1316. /*
  1317. * Some devices don't reliably have _HIDs & _CIDs, so add
  1318. * synthetic HIDs to make sure drivers can find them.
  1319. */
  1320. if (acpi_is_video_device(handle))
  1321. acpi_add_id(pnp, ACPI_VIDEO_HID);
  1322. else if (ACPI_SUCCESS(acpi_bay_match(handle)))
  1323. acpi_add_id(pnp, ACPI_BAY_HID);
  1324. else if (ACPI_SUCCESS(acpi_dock_match(handle)))
  1325. acpi_add_id(pnp, ACPI_DOCK_HID);
  1326. else if (!acpi_ibm_smbus_match(handle))
  1327. acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
  1328. else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
  1329. acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
  1330. strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
  1331. strcpy(pnp->device_class, ACPI_BUS_CLASS);
  1332. }
  1333. break;
  1334. case ACPI_BUS_TYPE_POWER:
  1335. acpi_add_id(pnp, ACPI_POWER_HID);
  1336. break;
  1337. case ACPI_BUS_TYPE_PROCESSOR:
  1338. acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
  1339. break;
  1340. case ACPI_BUS_TYPE_THERMAL:
  1341. acpi_add_id(pnp, ACPI_THERMAL_HID);
  1342. break;
  1343. case ACPI_BUS_TYPE_POWER_BUTTON:
  1344. acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
  1345. break;
  1346. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1347. acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
  1348. break;
  1349. }
  1350. }
  1351. void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
  1352. {
  1353. struct acpi_hardware_id *id, *tmp;
  1354. list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
  1355. kfree(id->id);
  1356. kfree(id);
  1357. }
  1358. kfree(pnp->unique_id);
  1359. }
  1360. void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
  1361. int type, unsigned long long sta)
  1362. {
  1363. INIT_LIST_HEAD(&device->pnp.ids);
  1364. device->device_type = type;
  1365. device->handle = handle;
  1366. device->parent = acpi_bus_get_parent(handle);
  1367. STRUCT_TO_INT(device->status) = sta;
  1368. acpi_device_get_busid(device);
  1369. acpi_set_pnp_ids(handle, &device->pnp, type);
  1370. acpi_bus_get_flags(device);
  1371. device->flags.match_driver = false;
  1372. device_initialize(&device->dev);
  1373. dev_set_uevent_suppress(&device->dev, true);
  1374. }
  1375. void acpi_device_add_finalize(struct acpi_device *device)
  1376. {
  1377. device->flags.match_driver = true;
  1378. dev_set_uevent_suppress(&device->dev, false);
  1379. kobject_uevent(&device->dev.kobj, KOBJ_ADD);
  1380. }
  1381. static int acpi_add_single_object(struct acpi_device **child,
  1382. acpi_handle handle, int type,
  1383. unsigned long long sta)
  1384. {
  1385. int result;
  1386. struct acpi_device *device;
  1387. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1388. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1389. if (!device) {
  1390. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1391. return -ENOMEM;
  1392. }
  1393. acpi_init_device_object(device, handle, type, sta);
  1394. acpi_bus_get_power_flags(device);
  1395. acpi_bus_get_wakeup_device_flags(device);
  1396. result = acpi_device_add(device, acpi_device_release);
  1397. if (result) {
  1398. acpi_device_release(&device->dev);
  1399. return result;
  1400. }
  1401. acpi_power_add_remove_device(device, true);
  1402. acpi_device_add_finalize(device);
  1403. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1404. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
  1405. dev_name(&device->dev), (char *) buffer.pointer,
  1406. device->parent ? dev_name(&device->parent->dev) : "(null)"));
  1407. kfree(buffer.pointer);
  1408. *child = device;
  1409. return 0;
  1410. }
  1411. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1412. unsigned long long *sta)
  1413. {
  1414. acpi_status status;
  1415. acpi_object_type acpi_type;
  1416. status = acpi_get_type(handle, &acpi_type);
  1417. if (ACPI_FAILURE(status))
  1418. return -ENODEV;
  1419. switch (acpi_type) {
  1420. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1421. case ACPI_TYPE_DEVICE:
  1422. *type = ACPI_BUS_TYPE_DEVICE;
  1423. status = acpi_bus_get_status_handle(handle, sta);
  1424. if (ACPI_FAILURE(status))
  1425. return -ENODEV;
  1426. break;
  1427. case ACPI_TYPE_PROCESSOR:
  1428. *type = ACPI_BUS_TYPE_PROCESSOR;
  1429. status = acpi_bus_get_status_handle(handle, sta);
  1430. if (ACPI_FAILURE(status))
  1431. return -ENODEV;
  1432. break;
  1433. case ACPI_TYPE_THERMAL:
  1434. *type = ACPI_BUS_TYPE_THERMAL;
  1435. *sta = ACPI_STA_DEFAULT;
  1436. break;
  1437. case ACPI_TYPE_POWER:
  1438. *type = ACPI_BUS_TYPE_POWER;
  1439. *sta = ACPI_STA_DEFAULT;
  1440. break;
  1441. default:
  1442. return -ENODEV;
  1443. }
  1444. return 0;
  1445. }
  1446. static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
  1447. char *idstr,
  1448. const struct acpi_device_id **matchid)
  1449. {
  1450. const struct acpi_device_id *devid;
  1451. for (devid = handler->ids; devid->id[0]; devid++)
  1452. if (!strcmp((char *)devid->id, idstr)) {
  1453. if (matchid)
  1454. *matchid = devid;
  1455. return true;
  1456. }
  1457. return false;
  1458. }
  1459. static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
  1460. const struct acpi_device_id **matchid)
  1461. {
  1462. struct acpi_scan_handler *handler;
  1463. list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
  1464. if (acpi_scan_handler_matching(handler, idstr, matchid))
  1465. return handler;
  1466. return NULL;
  1467. }
  1468. void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
  1469. {
  1470. if (!!hotplug->enabled == !!val)
  1471. return;
  1472. mutex_lock(&acpi_scan_lock);
  1473. hotplug->enabled = val;
  1474. mutex_unlock(&acpi_scan_lock);
  1475. }
  1476. static void acpi_scan_init_hotplug(acpi_handle handle, int type)
  1477. {
  1478. struct acpi_device_pnp pnp = {};
  1479. struct acpi_hardware_id *hwid;
  1480. struct acpi_scan_handler *handler;
  1481. INIT_LIST_HEAD(&pnp.ids);
  1482. acpi_set_pnp_ids(handle, &pnp, type);
  1483. if (!pnp.type.hardware_id)
  1484. goto out;
  1485. /*
  1486. * This relies on the fact that acpi_install_notify_handler() will not
  1487. * install the same notify handler routine twice for the same handle.
  1488. */
  1489. list_for_each_entry(hwid, &pnp.ids, list) {
  1490. handler = acpi_scan_match_handler(hwid->id, NULL);
  1491. if (handler) {
  1492. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1493. acpi_hotplug_notify_cb, handler);
  1494. break;
  1495. }
  1496. }
  1497. out:
  1498. acpi_free_pnp_ids(&pnp);
  1499. }
  1500. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
  1501. void *not_used, void **return_value)
  1502. {
  1503. struct acpi_device *device = NULL;
  1504. int type;
  1505. unsigned long long sta;
  1506. acpi_status status;
  1507. int result;
  1508. acpi_bus_get_device(handle, &device);
  1509. if (device)
  1510. goto out;
  1511. result = acpi_bus_type_and_status(handle, &type, &sta);
  1512. if (result)
  1513. return AE_OK;
  1514. if (type == ACPI_BUS_TYPE_POWER) {
  1515. acpi_add_power_resource(handle);
  1516. return AE_OK;
  1517. }
  1518. acpi_scan_init_hotplug(handle, type);
  1519. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1520. !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
  1521. struct acpi_device_wakeup wakeup;
  1522. acpi_handle temp;
  1523. status = acpi_get_handle(handle, "_PRW", &temp);
  1524. if (ACPI_SUCCESS(status)) {
  1525. acpi_bus_extract_wakeup_device_power_package(handle,
  1526. &wakeup);
  1527. acpi_power_resources_list_free(&wakeup.resources);
  1528. }
  1529. return AE_CTRL_DEPTH;
  1530. }
  1531. acpi_add_single_object(&device, handle, type, sta);
  1532. if (!device)
  1533. return AE_CTRL_DEPTH;
  1534. out:
  1535. if (!*return_value)
  1536. *return_value = device;
  1537. return AE_OK;
  1538. }
  1539. static int acpi_scan_attach_handler(struct acpi_device *device)
  1540. {
  1541. struct acpi_hardware_id *hwid;
  1542. int ret = 0;
  1543. list_for_each_entry(hwid, &device->pnp.ids, list) {
  1544. const struct acpi_device_id *devid;
  1545. struct acpi_scan_handler *handler;
  1546. handler = acpi_scan_match_handler(hwid->id, &devid);
  1547. if (handler) {
  1548. ret = handler->attach(device, devid);
  1549. if (ret > 0) {
  1550. device->handler = handler;
  1551. break;
  1552. } else if (ret < 0) {
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. return ret;
  1558. }
  1559. static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
  1560. void *not_used, void **ret_not_used)
  1561. {
  1562. struct acpi_device *device;
  1563. unsigned long long sta_not_used;
  1564. int ret;
  1565. /*
  1566. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1567. * namespace walks prematurely.
  1568. */
  1569. if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
  1570. return AE_OK;
  1571. if (acpi_bus_get_device(handle, &device))
  1572. return AE_CTRL_DEPTH;
  1573. ret = acpi_scan_attach_handler(device);
  1574. if (ret)
  1575. return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
  1576. ret = device_attach(&device->dev);
  1577. return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
  1578. }
  1579. /**
  1580. * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
  1581. * @handle: Root of the namespace scope to scan.
  1582. *
  1583. * Scan a given ACPI tree (probably recently hot-plugged) and create and add
  1584. * found devices.
  1585. *
  1586. * If no devices were found, -ENODEV is returned, but it does not mean that
  1587. * there has been a real error. There just have been no suitable ACPI objects
  1588. * in the table trunk from which the kernel could create a device and add an
  1589. * appropriate driver.
  1590. *
  1591. * Must be called under acpi_scan_lock.
  1592. */
  1593. int acpi_bus_scan(acpi_handle handle)
  1594. {
  1595. void *device = NULL;
  1596. int error = 0;
  1597. if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
  1598. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1599. acpi_bus_check_add, NULL, NULL, &device);
  1600. if (!device)
  1601. error = -ENODEV;
  1602. else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
  1603. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1604. acpi_bus_device_attach, NULL, NULL, NULL);
  1605. return error;
  1606. }
  1607. EXPORT_SYMBOL(acpi_bus_scan);
  1608. static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
  1609. void *not_used, void **ret_not_used)
  1610. {
  1611. struct acpi_device *device = NULL;
  1612. if (!acpi_bus_get_device(handle, &device)) {
  1613. struct acpi_scan_handler *dev_handler = device->handler;
  1614. device->removal_type = ACPI_BUS_REMOVAL_EJECT;
  1615. if (dev_handler) {
  1616. if (dev_handler->detach)
  1617. dev_handler->detach(device);
  1618. device->handler = NULL;
  1619. } else {
  1620. device_release_driver(&device->dev);
  1621. }
  1622. }
  1623. return AE_OK;
  1624. }
  1625. static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
  1626. void *not_used, void **ret_not_used)
  1627. {
  1628. struct acpi_device *device = NULL;
  1629. if (!acpi_bus_get_device(handle, &device))
  1630. acpi_device_unregister(device);
  1631. return AE_OK;
  1632. }
  1633. /**
  1634. * acpi_bus_trim - Remove ACPI device node and all of its descendants
  1635. * @start: Root of the ACPI device nodes subtree to remove.
  1636. *
  1637. * Must be called under acpi_scan_lock.
  1638. */
  1639. void acpi_bus_trim(struct acpi_device *start)
  1640. {
  1641. /*
  1642. * Execute acpi_bus_device_detach() as a post-order callback to detach
  1643. * all ACPI drivers from the device nodes being removed.
  1644. */
  1645. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1646. acpi_bus_device_detach, NULL, NULL);
  1647. acpi_bus_device_detach(start->handle, 0, NULL, NULL);
  1648. /*
  1649. * Execute acpi_bus_remove() as a post-order callback to remove device
  1650. * nodes in the given namespace scope.
  1651. */
  1652. acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
  1653. acpi_bus_remove, NULL, NULL);
  1654. acpi_bus_remove(start->handle, 0, NULL, NULL);
  1655. }
  1656. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1657. static int acpi_bus_scan_fixed(void)
  1658. {
  1659. int result = 0;
  1660. /*
  1661. * Enumerate all fixed-feature devices.
  1662. */
  1663. if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
  1664. struct acpi_device *device = NULL;
  1665. result = acpi_add_single_object(&device, NULL,
  1666. ACPI_BUS_TYPE_POWER_BUTTON,
  1667. ACPI_STA_DEFAULT);
  1668. if (result)
  1669. return result;
  1670. result = device_attach(&device->dev);
  1671. if (result < 0)
  1672. return result;
  1673. device_init_wakeup(&device->dev, true);
  1674. }
  1675. if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
  1676. struct acpi_device *device = NULL;
  1677. result = acpi_add_single_object(&device, NULL,
  1678. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1679. ACPI_STA_DEFAULT);
  1680. if (result)
  1681. return result;
  1682. result = device_attach(&device->dev);
  1683. }
  1684. return result < 0 ? result : 0;
  1685. }
  1686. int __init acpi_scan_init(void)
  1687. {
  1688. int result;
  1689. result = bus_register(&acpi_bus_type);
  1690. if (result) {
  1691. /* We don't want to quit even if we failed to add suspend/resume */
  1692. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1693. }
  1694. acpi_pci_root_init();
  1695. acpi_pci_link_init();
  1696. acpi_platform_init();
  1697. acpi_lpss_init();
  1698. acpi_container_init();
  1699. acpi_memory_hotplug_init();
  1700. mutex_lock(&acpi_scan_lock);
  1701. /*
  1702. * Enumerate devices in the ACPI namespace.
  1703. */
  1704. result = acpi_bus_scan(ACPI_ROOT_OBJECT);
  1705. if (result)
  1706. goto out;
  1707. result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
  1708. if (result)
  1709. goto out;
  1710. result = acpi_bus_scan_fixed();
  1711. if (result) {
  1712. acpi_device_unregister(acpi_root);
  1713. goto out;
  1714. }
  1715. acpi_update_all_gpes();
  1716. acpi_pci_root_hp_init();
  1717. out:
  1718. mutex_unlock(&acpi_scan_lock);
  1719. return result;
  1720. }