scan.c 62 KB

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