scan.c 55 KB

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