scan.c 58 KB

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