scan.c 51 KB

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