scan.c 65 KB

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