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