scan.c 68 KB

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