scan.c 38 KB

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