scan.c 46 KB

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