scan.c 56 KB

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