scan.c 53 KB

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