scan.c 42 KB

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