scan.c 66 KB

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