scan.c 60 KB

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