scan.c 39 KB

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