scan.c 61 KB

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