scan.c 53 KB

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