scan.c 59 KB

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