scan.c 53 KB

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