scan.c 59 KB

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