scan.c 59 KB

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