scan.c 55 KB

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