scan.c 63 KB

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