scan.c 53 KB

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