scan.c 55 KB

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