scan.c 70 KB

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