scan.c 42 KB

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