scan.c 44 KB

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