scan.c 70 KB

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