scan.c 53 KB

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