scan.c 53 KB

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