scan.c 57 KB

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