scan.c 40 KB

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