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