scan.c 56 KB

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