scan.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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->wakeup_list);
  418. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  419. if (!new_bus_id) {
  420. printk(KERN_ERR PREFIX "Memory allocation error\n");
  421. return -ENOMEM;
  422. }
  423. mutex_lock(&acpi_device_lock);
  424. /*
  425. * Find suitable bus_id and instance number in acpi_bus_id_list
  426. * If failed, create one and link it into acpi_bus_id_list
  427. */
  428. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  429. if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
  430. acpi_device_bus_id->instance_no ++;
  431. found = 1;
  432. kfree(new_bus_id);
  433. break;
  434. }
  435. }
  436. if(!found) {
  437. acpi_device_bus_id = new_bus_id;
  438. strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
  439. acpi_device_bus_id->instance_no = 0;
  440. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  441. }
  442. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  443. if (device->parent)
  444. list_add_tail(&device->node, &device->parent->children);
  445. if (device->wakeup.flags.valid)
  446. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  447. mutex_unlock(&acpi_device_lock);
  448. if (device->parent)
  449. device->dev.parent = &parent->dev;
  450. device->dev.bus = &acpi_bus_type;
  451. device_initialize(&device->dev);
  452. device->dev.release = &acpi_device_release;
  453. result = device_add(&device->dev);
  454. if(result) {
  455. dev_err(&device->dev, "Error adding device\n");
  456. goto end;
  457. }
  458. result = acpi_device_setup_files(device);
  459. if(result)
  460. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  461. dev_name(&device->dev));
  462. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  463. return 0;
  464. end:
  465. mutex_lock(&acpi_device_lock);
  466. if (device->parent)
  467. list_del(&device->node);
  468. list_del(&device->wakeup_list);
  469. mutex_unlock(&acpi_device_lock);
  470. return result;
  471. }
  472. static void acpi_device_unregister(struct acpi_device *device, int type)
  473. {
  474. mutex_lock(&acpi_device_lock);
  475. if (device->parent)
  476. list_del(&device->node);
  477. list_del(&device->wakeup_list);
  478. mutex_unlock(&acpi_device_lock);
  479. acpi_detach_data(device->handle, acpi_bus_data_handler);
  480. acpi_device_remove_files(device);
  481. device_unregister(&device->dev);
  482. }
  483. /* --------------------------------------------------------------------------
  484. Driver Management
  485. -------------------------------------------------------------------------- */
  486. /**
  487. * acpi_bus_driver_init - add a device to a driver
  488. * @device: the device to add and initialize
  489. * @driver: driver for the device
  490. *
  491. * Used to initialize a device via its device driver. Called whenever a
  492. * driver is bound to a device. Invokes the driver's add() ops.
  493. */
  494. static int
  495. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  496. {
  497. int result = 0;
  498. if (!device || !driver)
  499. return -EINVAL;
  500. if (!driver->ops.add)
  501. return -ENOSYS;
  502. result = driver->ops.add(device);
  503. if (result) {
  504. device->driver = NULL;
  505. device->driver_data = NULL;
  506. return result;
  507. }
  508. device->driver = driver;
  509. /*
  510. * TBD - Configuration Management: Assign resources to device based
  511. * upon possible configuration and currently allocated resources.
  512. */
  513. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  514. "Driver successfully bound to device\n"));
  515. return 0;
  516. }
  517. static int acpi_start_single_object(struct acpi_device *device)
  518. {
  519. int result = 0;
  520. struct acpi_driver *driver;
  521. if (!(driver = device->driver))
  522. return 0;
  523. if (driver->ops.start) {
  524. result = driver->ops.start(device);
  525. if (result && driver->ops.remove)
  526. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  527. }
  528. return result;
  529. }
  530. /**
  531. * acpi_bus_register_driver - register a driver with the ACPI bus
  532. * @driver: driver being registered
  533. *
  534. * Registers a driver with the ACPI bus. Searches the namespace for all
  535. * devices that match the driver's criteria and binds. Returns zero for
  536. * success or a negative error status for failure.
  537. */
  538. int acpi_bus_register_driver(struct acpi_driver *driver)
  539. {
  540. int ret;
  541. if (acpi_disabled)
  542. return -ENODEV;
  543. driver->drv.name = driver->name;
  544. driver->drv.bus = &acpi_bus_type;
  545. driver->drv.owner = driver->owner;
  546. ret = driver_register(&driver->drv);
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(acpi_bus_register_driver);
  550. /**
  551. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  552. * @driver: driver to unregister
  553. *
  554. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  555. * devices that match the driver's criteria and unbinds.
  556. */
  557. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  558. {
  559. driver_unregister(&driver->drv);
  560. }
  561. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  562. /* --------------------------------------------------------------------------
  563. Device Enumeration
  564. -------------------------------------------------------------------------- */
  565. acpi_status
  566. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  567. {
  568. acpi_status status;
  569. acpi_handle tmp;
  570. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  571. union acpi_object *obj;
  572. status = acpi_get_handle(handle, "_EJD", &tmp);
  573. if (ACPI_FAILURE(status))
  574. return status;
  575. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  576. if (ACPI_SUCCESS(status)) {
  577. obj = buffer.pointer;
  578. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  579. ejd);
  580. kfree(buffer.pointer);
  581. }
  582. return status;
  583. }
  584. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  585. void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
  586. {
  587. /* TBD */
  588. return;
  589. }
  590. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  591. {
  592. device->performance.state = ACPI_STATE_UNKNOWN;
  593. return 0;
  594. }
  595. static acpi_status
  596. acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
  597. union acpi_object *package)
  598. {
  599. int i = 0;
  600. union acpi_object *element = NULL;
  601. if (!device || !package || (package->package.count < 2))
  602. return AE_BAD_PARAMETER;
  603. element = &(package->package.elements[0]);
  604. if (!element)
  605. return AE_BAD_PARAMETER;
  606. if (element->type == ACPI_TYPE_PACKAGE) {
  607. if ((element->package.count < 2) ||
  608. (element->package.elements[0].type !=
  609. ACPI_TYPE_LOCAL_REFERENCE)
  610. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  611. return AE_BAD_DATA;
  612. device->wakeup.gpe_device =
  613. element->package.elements[0].reference.handle;
  614. device->wakeup.gpe_number =
  615. (u32) element->package.elements[1].integer.value;
  616. } else if (element->type == ACPI_TYPE_INTEGER) {
  617. device->wakeup.gpe_number = element->integer.value;
  618. } else
  619. return AE_BAD_DATA;
  620. element = &(package->package.elements[1]);
  621. if (element->type != ACPI_TYPE_INTEGER) {
  622. return AE_BAD_DATA;
  623. }
  624. device->wakeup.sleep_state = element->integer.value;
  625. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  626. return AE_NO_MEMORY;
  627. }
  628. device->wakeup.resources.count = package->package.count - 2;
  629. for (i = 0; i < device->wakeup.resources.count; i++) {
  630. element = &(package->package.elements[i + 2]);
  631. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  632. return AE_BAD_DATA;
  633. device->wakeup.resources.handles[i] = element->reference.handle;
  634. }
  635. return AE_OK;
  636. }
  637. static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  638. {
  639. acpi_status status = 0;
  640. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  641. union acpi_object *package = NULL;
  642. int psw_error;
  643. struct acpi_device_id button_device_ids[] = {
  644. {"PNP0C0D", 0},
  645. {"PNP0C0C", 0},
  646. {"PNP0C0E", 0},
  647. {"", 0},
  648. };
  649. /* _PRW */
  650. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  651. if (ACPI_FAILURE(status)) {
  652. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  653. goto end;
  654. }
  655. package = (union acpi_object *)buffer.pointer;
  656. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  657. if (ACPI_FAILURE(status)) {
  658. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  659. goto end;
  660. }
  661. kfree(buffer.pointer);
  662. device->wakeup.flags.valid = 1;
  663. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  664. * system for the ACPI device with the _PRW object.
  665. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  666. * So it is necessary to call _DSW object first. Only when it is not
  667. * present will the _PSW object used.
  668. */
  669. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  670. if (psw_error)
  671. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  672. "error in _DSW or _PSW evaluation\n"));
  673. /* Power button, Lid switch always enable wakeup */
  674. if (!acpi_match_device_ids(device, button_device_ids))
  675. device->wakeup.flags.run_wake = 1;
  676. end:
  677. if (ACPI_FAILURE(status))
  678. device->flags.wake_capable = 0;
  679. return 0;
  680. }
  681. static int acpi_bus_get_power_flags(struct acpi_device *device)
  682. {
  683. acpi_status status = 0;
  684. acpi_handle handle = NULL;
  685. u32 i = 0;
  686. /*
  687. * Power Management Flags
  688. */
  689. status = acpi_get_handle(device->handle, "_PSC", &handle);
  690. if (ACPI_SUCCESS(status))
  691. device->power.flags.explicit_get = 1;
  692. status = acpi_get_handle(device->handle, "_IRC", &handle);
  693. if (ACPI_SUCCESS(status))
  694. device->power.flags.inrush_current = 1;
  695. /*
  696. * Enumerate supported power management states
  697. */
  698. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  699. struct acpi_device_power_state *ps = &device->power.states[i];
  700. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  701. /* Evaluate "_PRx" to se if power resources are referenced */
  702. acpi_evaluate_reference(device->handle, object_name, NULL,
  703. &ps->resources);
  704. if (ps->resources.count) {
  705. device->power.flags.power_resources = 1;
  706. ps->flags.valid = 1;
  707. }
  708. /* Evaluate "_PSx" to see if we can do explicit sets */
  709. object_name[2] = 'S';
  710. status = acpi_get_handle(device->handle, object_name, &handle);
  711. if (ACPI_SUCCESS(status)) {
  712. ps->flags.explicit_set = 1;
  713. ps->flags.valid = 1;
  714. }
  715. /* State is valid if we have some power control */
  716. if (ps->resources.count || ps->flags.explicit_set)
  717. ps->flags.valid = 1;
  718. ps->power = -1; /* Unknown - driver assigned */
  719. ps->latency = -1; /* Unknown - driver assigned */
  720. }
  721. /* Set defaults for D0 and D3 states (always valid) */
  722. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  723. device->power.states[ACPI_STATE_D0].power = 100;
  724. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  725. device->power.states[ACPI_STATE_D3].power = 0;
  726. /* TBD: System wake support and resource requirements. */
  727. device->power.state = ACPI_STATE_UNKNOWN;
  728. acpi_bus_get_power(device->handle, &(device->power.state));
  729. return 0;
  730. }
  731. static int acpi_bus_get_flags(struct acpi_device *device)
  732. {
  733. acpi_status status = AE_OK;
  734. acpi_handle temp = NULL;
  735. /* Presence of _STA indicates 'dynamic_status' */
  736. status = acpi_get_handle(device->handle, "_STA", &temp);
  737. if (ACPI_SUCCESS(status))
  738. device->flags.dynamic_status = 1;
  739. /* Presence of _CID indicates 'compatible_ids' */
  740. status = acpi_get_handle(device->handle, "_CID", &temp);
  741. if (ACPI_SUCCESS(status))
  742. device->flags.compatible_ids = 1;
  743. /* Presence of _RMV indicates 'removable' */
  744. status = acpi_get_handle(device->handle, "_RMV", &temp);
  745. if (ACPI_SUCCESS(status))
  746. device->flags.removable = 1;
  747. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  748. status = acpi_get_handle(device->handle, "_EJD", &temp);
  749. if (ACPI_SUCCESS(status))
  750. device->flags.ejectable = 1;
  751. else {
  752. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  753. if (ACPI_SUCCESS(status))
  754. device->flags.ejectable = 1;
  755. }
  756. /* Presence of _LCK indicates 'lockable' */
  757. status = acpi_get_handle(device->handle, "_LCK", &temp);
  758. if (ACPI_SUCCESS(status))
  759. device->flags.lockable = 1;
  760. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  761. status = acpi_get_handle(device->handle, "_PS0", &temp);
  762. if (ACPI_FAILURE(status))
  763. status = acpi_get_handle(device->handle, "_PR0", &temp);
  764. if (ACPI_SUCCESS(status))
  765. device->flags.power_manageable = 1;
  766. /* Presence of _PRW indicates wake capable */
  767. status = acpi_get_handle(device->handle, "_PRW", &temp);
  768. if (ACPI_SUCCESS(status))
  769. device->flags.wake_capable = 1;
  770. /* TBD: Performance management */
  771. return 0;
  772. }
  773. static void acpi_device_get_busid(struct acpi_device *device,
  774. acpi_handle handle, int type)
  775. {
  776. char bus_id[5] = { '?', 0 };
  777. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  778. int i = 0;
  779. /*
  780. * Bus ID
  781. * ------
  782. * The device's Bus ID is simply the object name.
  783. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  784. */
  785. switch (type) {
  786. case ACPI_BUS_TYPE_SYSTEM:
  787. strcpy(device->pnp.bus_id, "ACPI");
  788. break;
  789. case ACPI_BUS_TYPE_POWER_BUTTON:
  790. strcpy(device->pnp.bus_id, "PWRF");
  791. break;
  792. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  793. strcpy(device->pnp.bus_id, "SLPF");
  794. break;
  795. default:
  796. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  797. /* Clean up trailing underscores (if any) */
  798. for (i = 3; i > 1; i--) {
  799. if (bus_id[i] == '_')
  800. bus_id[i] = '\0';
  801. else
  802. break;
  803. }
  804. strcpy(device->pnp.bus_id, bus_id);
  805. break;
  806. }
  807. }
  808. /*
  809. * acpi_bay_match - see if a device is an ejectable driver bay
  810. *
  811. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  812. * then we can safely call it an ejectable drive bay
  813. */
  814. static int acpi_bay_match(struct acpi_device *device){
  815. acpi_status status;
  816. acpi_handle handle;
  817. acpi_handle tmp;
  818. acpi_handle phandle;
  819. handle = device->handle;
  820. status = acpi_get_handle(handle, "_EJ0", &tmp);
  821. if (ACPI_FAILURE(status))
  822. return -ENODEV;
  823. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  824. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  825. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  826. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  827. return 0;
  828. if (acpi_get_parent(handle, &phandle))
  829. return -ENODEV;
  830. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  831. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  832. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  833. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  834. return 0;
  835. return -ENODEV;
  836. }
  837. /*
  838. * acpi_dock_match - see if a device has a _DCK method
  839. */
  840. static int acpi_dock_match(struct acpi_device *device)
  841. {
  842. acpi_handle tmp;
  843. return acpi_get_handle(device->handle, "_DCK", &tmp);
  844. }
  845. static void acpi_device_set_id(struct acpi_device *device,
  846. struct acpi_device *parent, acpi_handle handle,
  847. int type)
  848. {
  849. struct acpi_device_info *info;
  850. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  851. char *hid = NULL;
  852. char *uid = NULL;
  853. struct acpi_compatible_id_list *cid_list = NULL;
  854. const char *cid_add = NULL;
  855. acpi_status status;
  856. switch (type) {
  857. case ACPI_BUS_TYPE_DEVICE:
  858. status = acpi_get_object_info(handle, &buffer);
  859. if (ACPI_FAILURE(status)) {
  860. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  861. return;
  862. }
  863. info = buffer.pointer;
  864. if (info->valid & ACPI_VALID_HID)
  865. hid = info->hardware_id.value;
  866. if (info->valid & ACPI_VALID_UID)
  867. uid = info->unique_id.value;
  868. if (info->valid & ACPI_VALID_CID)
  869. cid_list = &info->compatibility_id;
  870. if (info->valid & ACPI_VALID_ADR) {
  871. device->pnp.bus_address = info->address;
  872. device->flags.bus_address = 1;
  873. }
  874. /* If we have a video/bay/dock device, add our selfdefined
  875. HID to the CID list. Like that the video/bay/dock drivers
  876. will get autoloaded and the device might still match
  877. against another driver.
  878. */
  879. if (acpi_is_video_device(device))
  880. cid_add = ACPI_VIDEO_HID;
  881. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  882. cid_add = ACPI_BAY_HID;
  883. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  884. cid_add = ACPI_DOCK_HID;
  885. break;
  886. case ACPI_BUS_TYPE_POWER:
  887. hid = ACPI_POWER_HID;
  888. break;
  889. case ACPI_BUS_TYPE_PROCESSOR:
  890. hid = ACPI_PROCESSOR_OBJECT_HID;
  891. break;
  892. case ACPI_BUS_TYPE_SYSTEM:
  893. hid = ACPI_SYSTEM_HID;
  894. break;
  895. case ACPI_BUS_TYPE_THERMAL:
  896. hid = ACPI_THERMAL_HID;
  897. break;
  898. case ACPI_BUS_TYPE_POWER_BUTTON:
  899. hid = ACPI_BUTTON_HID_POWERF;
  900. break;
  901. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  902. hid = ACPI_BUTTON_HID_SLEEPF;
  903. break;
  904. }
  905. /*
  906. * \_SB
  907. * ----
  908. * Fix for the system root bus device -- the only root-level device.
  909. */
  910. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  911. hid = ACPI_BUS_HID;
  912. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  913. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  914. }
  915. if (hid) {
  916. strcpy(device->pnp.hardware_id, hid);
  917. device->flags.hardware_id = 1;
  918. }
  919. if (uid) {
  920. strcpy(device->pnp.unique_id, uid);
  921. device->flags.unique_id = 1;
  922. }
  923. if (cid_list || cid_add) {
  924. struct acpi_compatible_id_list *list;
  925. int size = 0;
  926. int count = 0;
  927. if (cid_list) {
  928. size = cid_list->size;
  929. } else if (cid_add) {
  930. size = sizeof(struct acpi_compatible_id_list);
  931. cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
  932. if (!cid_list) {
  933. printk(KERN_ERR "Memory allocation error\n");
  934. kfree(buffer.pointer);
  935. return;
  936. } else {
  937. cid_list->count = 0;
  938. cid_list->size = size;
  939. }
  940. }
  941. if (cid_add)
  942. size += sizeof(struct acpi_compatible_id);
  943. list = kmalloc(size, GFP_KERNEL);
  944. if (list) {
  945. if (cid_list) {
  946. memcpy(list, cid_list, cid_list->size);
  947. count = cid_list->count;
  948. }
  949. if (cid_add) {
  950. strncpy(list->id[count].value, cid_add,
  951. ACPI_MAX_CID_LENGTH);
  952. count++;
  953. device->flags.compatible_ids = 1;
  954. }
  955. list->size = size;
  956. list->count = count;
  957. device->pnp.cid_list = list;
  958. } else
  959. printk(KERN_ERR PREFIX "Memory allocation error\n");
  960. }
  961. kfree(buffer.pointer);
  962. }
  963. static int acpi_device_set_context(struct acpi_device *device, int type)
  964. {
  965. acpi_status status = AE_OK;
  966. int result = 0;
  967. /*
  968. * Context
  969. * -------
  970. * Attach this 'struct acpi_device' to the ACPI object. This makes
  971. * resolutions from handle->device very efficient. Note that we need
  972. * to be careful with fixed-feature devices as they all attach to the
  973. * root object.
  974. */
  975. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  976. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  977. status = acpi_attach_data(device->handle,
  978. acpi_bus_data_handler, device);
  979. if (ACPI_FAILURE(status)) {
  980. printk(KERN_ERR PREFIX "Error attaching device data\n");
  981. result = -ENODEV;
  982. }
  983. }
  984. return result;
  985. }
  986. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  987. {
  988. if (!dev)
  989. return -EINVAL;
  990. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  991. device_release_driver(&dev->dev);
  992. if (!rmdevice)
  993. return 0;
  994. /*
  995. * unbind _ADR-Based Devices when hot removal
  996. */
  997. if (dev->flags.bus_address) {
  998. if ((dev->parent) && (dev->parent->ops.unbind))
  999. dev->parent->ops.unbind(dev);
  1000. }
  1001. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  1002. return 0;
  1003. }
  1004. static int
  1005. acpi_add_single_object(struct acpi_device **child,
  1006. struct acpi_device *parent, acpi_handle handle, int type,
  1007. struct acpi_bus_ops *ops)
  1008. {
  1009. int result = 0;
  1010. struct acpi_device *device = NULL;
  1011. if (!child)
  1012. return -EINVAL;
  1013. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1014. if (!device) {
  1015. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1016. return -ENOMEM;
  1017. }
  1018. device->handle = handle;
  1019. device->parent = parent;
  1020. device->bus_ops = *ops; /* workround for not call .start */
  1021. acpi_device_get_busid(device, handle, type);
  1022. /*
  1023. * Flags
  1024. * -----
  1025. * Get prior to calling acpi_bus_get_status() so we know whether
  1026. * or not _STA is present. Note that we only look for object
  1027. * handles -- cannot evaluate objects until we know the device is
  1028. * present and properly initialized.
  1029. */
  1030. result = acpi_bus_get_flags(device);
  1031. if (result)
  1032. goto end;
  1033. /*
  1034. * Status
  1035. * ------
  1036. * See if the device is present. We always assume that non-Device
  1037. * and non-Processor objects (e.g. thermal zones, power resources,
  1038. * etc.) are present, functioning, etc. (at least when parent object
  1039. * is present). Note that _STA has a different meaning for some
  1040. * objects (e.g. power resources) so we need to be careful how we use
  1041. * it.
  1042. */
  1043. switch (type) {
  1044. case ACPI_BUS_TYPE_PROCESSOR:
  1045. case ACPI_BUS_TYPE_DEVICE:
  1046. result = acpi_bus_get_status(device);
  1047. if (ACPI_FAILURE(result)) {
  1048. result = -ENODEV;
  1049. goto end;
  1050. }
  1051. /*
  1052. * When the device is neither present nor functional, the
  1053. * device should not be added to Linux ACPI device tree.
  1054. * When the status of the device is not present but functinal,
  1055. * it should be added to Linux ACPI tree. For example : bay
  1056. * device , dock device.
  1057. * In such conditions it is unncessary to check whether it is
  1058. * bay device or dock device.
  1059. */
  1060. if (!device->status.present && !device->status.functional) {
  1061. result = -ENODEV;
  1062. goto end;
  1063. }
  1064. break;
  1065. default:
  1066. STRUCT_TO_INT(device->status) =
  1067. ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  1068. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  1069. break;
  1070. }
  1071. /*
  1072. * Initialize Device
  1073. * -----------------
  1074. * TBD: Synch with Core's enumeration/initialization process.
  1075. */
  1076. /*
  1077. * Hardware ID, Unique ID, & Bus Address
  1078. * -------------------------------------
  1079. */
  1080. acpi_device_set_id(device, parent, handle, type);
  1081. /*
  1082. * The ACPI device is attached to acpi handle before getting
  1083. * the power/wakeup/peformance flags. Otherwise OS can't get
  1084. * the corresponding ACPI device by the acpi handle in the course
  1085. * of getting the power/wakeup/performance flags.
  1086. */
  1087. result = acpi_device_set_context(device, type);
  1088. if (result)
  1089. goto end;
  1090. /*
  1091. * Power Management
  1092. * ----------------
  1093. */
  1094. if (device->flags.power_manageable) {
  1095. result = acpi_bus_get_power_flags(device);
  1096. if (result)
  1097. goto end;
  1098. }
  1099. /*
  1100. * Wakeup device management
  1101. *-----------------------
  1102. */
  1103. if (device->flags.wake_capable) {
  1104. result = acpi_bus_get_wakeup_device_flags(device);
  1105. if (result)
  1106. goto end;
  1107. }
  1108. /*
  1109. * Performance Management
  1110. * ----------------------
  1111. */
  1112. if (device->flags.performance_manageable) {
  1113. result = acpi_bus_get_perf_flags(device);
  1114. if (result)
  1115. goto end;
  1116. }
  1117. result = acpi_device_register(device, parent);
  1118. /*
  1119. * Bind _ADR-Based Devices when hot add
  1120. */
  1121. if (device->flags.bus_address) {
  1122. if (device->parent && device->parent->ops.bind)
  1123. device->parent->ops.bind(device);
  1124. }
  1125. end:
  1126. if (!result)
  1127. *child = device;
  1128. else {
  1129. kfree(device->pnp.cid_list);
  1130. kfree(device);
  1131. }
  1132. return result;
  1133. }
  1134. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  1135. {
  1136. acpi_status status = AE_OK;
  1137. struct acpi_device *parent = NULL;
  1138. struct acpi_device *child = NULL;
  1139. acpi_handle phandle = NULL;
  1140. acpi_handle chandle = NULL;
  1141. acpi_object_type type = 0;
  1142. u32 level = 1;
  1143. if (!start)
  1144. return -EINVAL;
  1145. parent = start;
  1146. phandle = start->handle;
  1147. /*
  1148. * Parse through the ACPI namespace, identify all 'devices', and
  1149. * create a new 'struct acpi_device' for each.
  1150. */
  1151. while ((level > 0) && parent) {
  1152. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1153. chandle, &chandle);
  1154. /*
  1155. * If this scope is exhausted then move our way back up.
  1156. */
  1157. if (ACPI_FAILURE(status)) {
  1158. level--;
  1159. chandle = phandle;
  1160. acpi_get_parent(phandle, &phandle);
  1161. if (parent->parent)
  1162. parent = parent->parent;
  1163. continue;
  1164. }
  1165. status = acpi_get_type(chandle, &type);
  1166. if (ACPI_FAILURE(status))
  1167. continue;
  1168. /*
  1169. * If this is a scope object then parse it (depth-first).
  1170. */
  1171. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  1172. level++;
  1173. phandle = chandle;
  1174. chandle = NULL;
  1175. continue;
  1176. }
  1177. /*
  1178. * We're only interested in objects that we consider 'devices'.
  1179. */
  1180. switch (type) {
  1181. case ACPI_TYPE_DEVICE:
  1182. type = ACPI_BUS_TYPE_DEVICE;
  1183. break;
  1184. case ACPI_TYPE_PROCESSOR:
  1185. type = ACPI_BUS_TYPE_PROCESSOR;
  1186. break;
  1187. case ACPI_TYPE_THERMAL:
  1188. type = ACPI_BUS_TYPE_THERMAL;
  1189. break;
  1190. case ACPI_TYPE_POWER:
  1191. type = ACPI_BUS_TYPE_POWER;
  1192. break;
  1193. default:
  1194. continue;
  1195. }
  1196. if (ops->acpi_op_add)
  1197. status = acpi_add_single_object(&child, parent,
  1198. chandle, type, ops);
  1199. else
  1200. status = acpi_bus_get_device(chandle, &child);
  1201. if (ACPI_FAILURE(status))
  1202. continue;
  1203. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1204. status = acpi_start_single_object(child);
  1205. if (ACPI_FAILURE(status))
  1206. continue;
  1207. }
  1208. /*
  1209. * If the device is present, enabled, and functioning then
  1210. * parse its scope (depth-first). Note that we need to
  1211. * represent absent devices to facilitate PnP notifications
  1212. * -- but only the subtree head (not all of its children,
  1213. * which will be enumerated when the parent is inserted).
  1214. *
  1215. * TBD: Need notifications and other detection mechanisms
  1216. * in place before we can fully implement this.
  1217. */
  1218. /*
  1219. * When the device is not present but functional, it is also
  1220. * necessary to scan the children of this device.
  1221. */
  1222. if (child->status.present || (!child->status.present &&
  1223. child->status.functional)) {
  1224. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1225. NULL, NULL);
  1226. if (ACPI_SUCCESS(status)) {
  1227. level++;
  1228. phandle = chandle;
  1229. chandle = NULL;
  1230. parent = child;
  1231. }
  1232. }
  1233. }
  1234. return 0;
  1235. }
  1236. int
  1237. acpi_bus_add(struct acpi_device **child,
  1238. struct acpi_device *parent, acpi_handle handle, int type)
  1239. {
  1240. int result;
  1241. struct acpi_bus_ops ops;
  1242. memset(&ops, 0, sizeof(ops));
  1243. ops.acpi_op_add = 1;
  1244. result = acpi_add_single_object(child, parent, handle, type, &ops);
  1245. if (!result)
  1246. result = acpi_bus_scan(*child, &ops);
  1247. return result;
  1248. }
  1249. EXPORT_SYMBOL(acpi_bus_add);
  1250. int acpi_bus_start(struct acpi_device *device)
  1251. {
  1252. int result;
  1253. struct acpi_bus_ops ops;
  1254. if (!device)
  1255. return -EINVAL;
  1256. result = acpi_start_single_object(device);
  1257. if (!result) {
  1258. memset(&ops, 0, sizeof(ops));
  1259. ops.acpi_op_start = 1;
  1260. result = acpi_bus_scan(device, &ops);
  1261. }
  1262. return result;
  1263. }
  1264. EXPORT_SYMBOL(acpi_bus_start);
  1265. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1266. {
  1267. acpi_status status;
  1268. struct acpi_device *parent, *child;
  1269. acpi_handle phandle, chandle;
  1270. acpi_object_type type;
  1271. u32 level = 1;
  1272. int err = 0;
  1273. parent = start;
  1274. phandle = start->handle;
  1275. child = chandle = NULL;
  1276. while ((level > 0) && parent && (!err)) {
  1277. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1278. chandle, &chandle);
  1279. /*
  1280. * If this scope is exhausted then move our way back up.
  1281. */
  1282. if (ACPI_FAILURE(status)) {
  1283. level--;
  1284. chandle = phandle;
  1285. acpi_get_parent(phandle, &phandle);
  1286. child = parent;
  1287. parent = parent->parent;
  1288. if (level == 0)
  1289. err = acpi_bus_remove(child, rmdevice);
  1290. else
  1291. err = acpi_bus_remove(child, 1);
  1292. continue;
  1293. }
  1294. status = acpi_get_type(chandle, &type);
  1295. if (ACPI_FAILURE(status)) {
  1296. continue;
  1297. }
  1298. /*
  1299. * If there is a device corresponding to chandle then
  1300. * parse it (depth-first).
  1301. */
  1302. if (acpi_bus_get_device(chandle, &child) == 0) {
  1303. level++;
  1304. phandle = chandle;
  1305. chandle = NULL;
  1306. parent = child;
  1307. }
  1308. continue;
  1309. }
  1310. return err;
  1311. }
  1312. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1313. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1314. {
  1315. int result = 0;
  1316. struct acpi_device *device = NULL;
  1317. struct acpi_bus_ops ops;
  1318. if (!root)
  1319. return -ENODEV;
  1320. memset(&ops, 0, sizeof(ops));
  1321. ops.acpi_op_add = 1;
  1322. ops.acpi_op_start = 1;
  1323. /*
  1324. * Enumerate all fixed-feature devices.
  1325. */
  1326. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1327. result = acpi_add_single_object(&device, acpi_root,
  1328. NULL,
  1329. ACPI_BUS_TYPE_POWER_BUTTON,
  1330. &ops);
  1331. }
  1332. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1333. result = acpi_add_single_object(&device, acpi_root,
  1334. NULL,
  1335. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1336. &ops);
  1337. }
  1338. return result;
  1339. }
  1340. int __init acpi_scan_init(void)
  1341. {
  1342. int result;
  1343. struct acpi_bus_ops ops;
  1344. memset(&ops, 0, sizeof(ops));
  1345. ops.acpi_op_add = 1;
  1346. ops.acpi_op_start = 1;
  1347. result = bus_register(&acpi_bus_type);
  1348. if (result) {
  1349. /* We don't want to quit even if we failed to add suspend/resume */
  1350. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1351. }
  1352. /*
  1353. * Create the root device in the bus's device tree
  1354. */
  1355. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1356. ACPI_BUS_TYPE_SYSTEM, &ops);
  1357. if (result)
  1358. goto Done;
  1359. /*
  1360. * Enumerate devices in the ACPI namespace.
  1361. */
  1362. result = acpi_bus_scan_fixed(acpi_root);
  1363. if (!result)
  1364. result = acpi_bus_scan(acpi_root, &ops);
  1365. if (result)
  1366. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1367. Done:
  1368. return result;
  1369. }