scan.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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_dummy1;
  741. acpi_handle h_dummy2;
  742. acpi_handle h_dummy3;
  743. if (!device)
  744. return -EINVAL;
  745. /* Since there is no HID, CID for ACPI Video drivers, we have
  746. * to check well known required nodes for each feature we support.
  747. */
  748. /* Does this device able to support video switching ? */
  749. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  750. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  751. return 0;
  752. /* Does this device able to retrieve a video ROM ? */
  753. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  754. return 0;
  755. /* Does this device able to configure which video head to be POSTed ? */
  756. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  757. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  758. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  759. return 0;
  760. return -ENODEV;
  761. }
  762. /*
  763. * acpi_bay_match - see if a device is an ejectable driver bay
  764. *
  765. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  766. * then we can safely call it an ejectable drive bay
  767. */
  768. static int acpi_bay_match(struct acpi_device *device){
  769. acpi_status status;
  770. acpi_handle handle;
  771. acpi_handle tmp;
  772. acpi_handle phandle;
  773. handle = device->handle;
  774. status = acpi_get_handle(handle, "_EJ0", &tmp);
  775. if (ACPI_FAILURE(status))
  776. return -ENODEV;
  777. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  778. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  779. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  780. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  781. return 0;
  782. if (acpi_get_parent(handle, &phandle))
  783. return -ENODEV;
  784. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  785. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  786. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  787. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  788. return 0;
  789. return -ENODEV;
  790. }
  791. /*
  792. * acpi_dock_match - see if a device has a _DCK method
  793. */
  794. static int acpi_dock_match(struct acpi_device *device)
  795. {
  796. acpi_handle tmp;
  797. return acpi_get_handle(device->handle, "_DCK", &tmp);
  798. }
  799. static void acpi_device_set_id(struct acpi_device *device,
  800. struct acpi_device *parent, acpi_handle handle,
  801. int type)
  802. {
  803. struct acpi_device_info *info;
  804. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  805. char *hid = NULL;
  806. char *uid = NULL;
  807. struct acpi_compatible_id_list *cid_list = NULL;
  808. const char *cid_add = NULL;
  809. acpi_status status;
  810. switch (type) {
  811. case ACPI_BUS_TYPE_DEVICE:
  812. status = acpi_get_object_info(handle, &buffer);
  813. if (ACPI_FAILURE(status)) {
  814. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  815. return;
  816. }
  817. info = buffer.pointer;
  818. if (info->valid & ACPI_VALID_HID)
  819. hid = info->hardware_id.value;
  820. if (info->valid & ACPI_VALID_UID)
  821. uid = info->unique_id.value;
  822. if (info->valid & ACPI_VALID_CID)
  823. cid_list = &info->compatibility_id;
  824. if (info->valid & ACPI_VALID_ADR) {
  825. device->pnp.bus_address = info->address;
  826. device->flags.bus_address = 1;
  827. }
  828. /* If we have a video/bay/dock device, add our selfdefined
  829. HID to the CID list. Like that the video/bay/dock drivers
  830. will get autoloaded and the device might still match
  831. against another driver.
  832. */
  833. if (ACPI_SUCCESS(acpi_video_bus_match(device)))
  834. cid_add = ACPI_VIDEO_HID;
  835. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  836. cid_add = ACPI_BAY_HID;
  837. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  838. cid_add = ACPI_DOCK_HID;
  839. break;
  840. case ACPI_BUS_TYPE_POWER:
  841. hid = ACPI_POWER_HID;
  842. break;
  843. case ACPI_BUS_TYPE_PROCESSOR:
  844. hid = ACPI_PROCESSOR_HID;
  845. break;
  846. case ACPI_BUS_TYPE_SYSTEM:
  847. hid = ACPI_SYSTEM_HID;
  848. break;
  849. case ACPI_BUS_TYPE_THERMAL:
  850. hid = ACPI_THERMAL_HID;
  851. break;
  852. case ACPI_BUS_TYPE_POWER_BUTTON:
  853. hid = ACPI_BUTTON_HID_POWERF;
  854. break;
  855. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  856. hid = ACPI_BUTTON_HID_SLEEPF;
  857. break;
  858. }
  859. /*
  860. * \_SB
  861. * ----
  862. * Fix for the system root bus device -- the only root-level device.
  863. */
  864. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  865. hid = ACPI_BUS_HID;
  866. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  867. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  868. }
  869. if (hid) {
  870. strcpy(device->pnp.hardware_id, hid);
  871. device->flags.hardware_id = 1;
  872. }
  873. if (uid) {
  874. strcpy(device->pnp.unique_id, uid);
  875. device->flags.unique_id = 1;
  876. }
  877. if (cid_list || cid_add) {
  878. struct acpi_compatible_id_list *list;
  879. int size = 0;
  880. int count = 0;
  881. if (cid_list) {
  882. size = cid_list->size;
  883. } else if (cid_add) {
  884. size = sizeof(struct acpi_compatible_id_list);
  885. cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
  886. if (!cid_list) {
  887. printk(KERN_ERR "Memory allocation error\n");
  888. kfree(buffer.pointer);
  889. return;
  890. } else {
  891. cid_list->count = 0;
  892. cid_list->size = size;
  893. }
  894. }
  895. if (cid_add)
  896. size += sizeof(struct acpi_compatible_id);
  897. list = kmalloc(size, GFP_KERNEL);
  898. if (list) {
  899. if (cid_list) {
  900. memcpy(list, cid_list, cid_list->size);
  901. count = cid_list->count;
  902. }
  903. if (cid_add) {
  904. strncpy(list->id[count].value, cid_add,
  905. ACPI_MAX_CID_LENGTH);
  906. count++;
  907. device->flags.compatible_ids = 1;
  908. }
  909. list->size = size;
  910. list->count = count;
  911. device->pnp.cid_list = list;
  912. } else
  913. printk(KERN_ERR PREFIX "Memory allocation error\n");
  914. }
  915. kfree(buffer.pointer);
  916. }
  917. static int acpi_device_set_context(struct acpi_device *device, int type)
  918. {
  919. acpi_status status = AE_OK;
  920. int result = 0;
  921. /*
  922. * Context
  923. * -------
  924. * Attach this 'struct acpi_device' to the ACPI object. This makes
  925. * resolutions from handle->device very efficient. Note that we need
  926. * to be careful with fixed-feature devices as they all attach to the
  927. * root object.
  928. */
  929. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  930. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  931. status = acpi_attach_data(device->handle,
  932. acpi_bus_data_handler, device);
  933. if (ACPI_FAILURE(status)) {
  934. printk(KERN_ERR PREFIX "Error attaching device data\n");
  935. result = -ENODEV;
  936. }
  937. }
  938. return result;
  939. }
  940. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  941. {
  942. if (!dev)
  943. return -EINVAL;
  944. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  945. device_release_driver(&dev->dev);
  946. if (!rmdevice)
  947. return 0;
  948. /*
  949. * unbind _ADR-Based Devices when hot removal
  950. */
  951. if (dev->flags.bus_address) {
  952. if ((dev->parent) && (dev->parent->ops.unbind))
  953. dev->parent->ops.unbind(dev);
  954. }
  955. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  956. return 0;
  957. }
  958. static int
  959. acpi_is_child_device(struct acpi_device *device,
  960. int (*matcher)(struct acpi_device *))
  961. {
  962. int result = -ENODEV;
  963. do {
  964. if (ACPI_SUCCESS(matcher(device)))
  965. return AE_OK;
  966. } while ((device = device->parent));
  967. return result;
  968. }
  969. static int
  970. acpi_add_single_object(struct acpi_device **child,
  971. struct acpi_device *parent, acpi_handle handle, int type,
  972. struct acpi_bus_ops *ops)
  973. {
  974. int result = 0;
  975. struct acpi_device *device = NULL;
  976. if (!child)
  977. return -EINVAL;
  978. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  979. if (!device) {
  980. printk(KERN_ERR PREFIX "Memory allocation error\n");
  981. return -ENOMEM;
  982. }
  983. device->handle = handle;
  984. device->parent = parent;
  985. device->bus_ops = *ops; /* workround for not call .start */
  986. acpi_device_get_busid(device, handle, type);
  987. /*
  988. * Flags
  989. * -----
  990. * Get prior to calling acpi_bus_get_status() so we know whether
  991. * or not _STA is present. Note that we only look for object
  992. * handles -- cannot evaluate objects until we know the device is
  993. * present and properly initialized.
  994. */
  995. result = acpi_bus_get_flags(device);
  996. if (result)
  997. goto end;
  998. /*
  999. * Status
  1000. * ------
  1001. * See if the device is present. We always assume that non-Device
  1002. * and non-Processor objects (e.g. thermal zones, power resources,
  1003. * etc.) are present, functioning, etc. (at least when parent object
  1004. * is present). Note that _STA has a different meaning for some
  1005. * objects (e.g. power resources) so we need to be careful how we use
  1006. * it.
  1007. */
  1008. switch (type) {
  1009. case ACPI_BUS_TYPE_PROCESSOR:
  1010. case ACPI_BUS_TYPE_DEVICE:
  1011. result = acpi_bus_get_status(device);
  1012. if (ACPI_FAILURE(result)) {
  1013. result = -ENODEV;
  1014. goto end;
  1015. }
  1016. if (!device->status.present) {
  1017. /* Bay and dock should be handled even if absent */
  1018. if (!ACPI_SUCCESS(
  1019. acpi_is_child_device(device, acpi_bay_match)) &&
  1020. !ACPI_SUCCESS(
  1021. acpi_is_child_device(device, acpi_dock_match))) {
  1022. result = -ENODEV;
  1023. goto end;
  1024. }
  1025. }
  1026. break;
  1027. default:
  1028. STRUCT_TO_INT(device->status) =
  1029. ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  1030. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  1031. break;
  1032. }
  1033. /*
  1034. * Initialize Device
  1035. * -----------------
  1036. * TBD: Synch with Core's enumeration/initialization process.
  1037. */
  1038. /*
  1039. * Hardware ID, Unique ID, & Bus Address
  1040. * -------------------------------------
  1041. */
  1042. acpi_device_set_id(device, parent, handle, type);
  1043. /*
  1044. * Power Management
  1045. * ----------------
  1046. */
  1047. if (device->flags.power_manageable) {
  1048. result = acpi_bus_get_power_flags(device);
  1049. if (result)
  1050. goto end;
  1051. }
  1052. /*
  1053. * Wakeup device management
  1054. *-----------------------
  1055. */
  1056. if (device->flags.wake_capable) {
  1057. result = acpi_bus_get_wakeup_device_flags(device);
  1058. if (result)
  1059. goto end;
  1060. }
  1061. /*
  1062. * Performance Management
  1063. * ----------------------
  1064. */
  1065. if (device->flags.performance_manageable) {
  1066. result = acpi_bus_get_perf_flags(device);
  1067. if (result)
  1068. goto end;
  1069. }
  1070. if ((result = acpi_device_set_context(device, type)))
  1071. goto end;
  1072. result = acpi_device_register(device, parent);
  1073. /*
  1074. * Bind _ADR-Based Devices when hot add
  1075. */
  1076. if (device->flags.bus_address) {
  1077. if (device->parent && device->parent->ops.bind)
  1078. device->parent->ops.bind(device);
  1079. }
  1080. end:
  1081. if (!result)
  1082. *child = device;
  1083. else {
  1084. kfree(device->pnp.cid_list);
  1085. kfree(device);
  1086. }
  1087. return result;
  1088. }
  1089. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  1090. {
  1091. acpi_status status = AE_OK;
  1092. struct acpi_device *parent = NULL;
  1093. struct acpi_device *child = NULL;
  1094. acpi_handle phandle = NULL;
  1095. acpi_handle chandle = NULL;
  1096. acpi_object_type type = 0;
  1097. u32 level = 1;
  1098. if (!start)
  1099. return -EINVAL;
  1100. parent = start;
  1101. phandle = start->handle;
  1102. /*
  1103. * Parse through the ACPI namespace, identify all 'devices', and
  1104. * create a new 'struct acpi_device' for each.
  1105. */
  1106. while ((level > 0) && parent) {
  1107. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1108. chandle, &chandle);
  1109. /*
  1110. * If this scope is exhausted then move our way back up.
  1111. */
  1112. if (ACPI_FAILURE(status)) {
  1113. level--;
  1114. chandle = phandle;
  1115. acpi_get_parent(phandle, &phandle);
  1116. if (parent->parent)
  1117. parent = parent->parent;
  1118. continue;
  1119. }
  1120. status = acpi_get_type(chandle, &type);
  1121. if (ACPI_FAILURE(status))
  1122. continue;
  1123. /*
  1124. * If this is a scope object then parse it (depth-first).
  1125. */
  1126. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  1127. level++;
  1128. phandle = chandle;
  1129. chandle = NULL;
  1130. continue;
  1131. }
  1132. /*
  1133. * We're only interested in objects that we consider 'devices'.
  1134. */
  1135. switch (type) {
  1136. case ACPI_TYPE_DEVICE:
  1137. type = ACPI_BUS_TYPE_DEVICE;
  1138. break;
  1139. case ACPI_TYPE_PROCESSOR:
  1140. type = ACPI_BUS_TYPE_PROCESSOR;
  1141. break;
  1142. case ACPI_TYPE_THERMAL:
  1143. type = ACPI_BUS_TYPE_THERMAL;
  1144. break;
  1145. case ACPI_TYPE_POWER:
  1146. type = ACPI_BUS_TYPE_POWER;
  1147. break;
  1148. default:
  1149. continue;
  1150. }
  1151. if (ops->acpi_op_add)
  1152. status = acpi_add_single_object(&child, parent,
  1153. chandle, type, ops);
  1154. else
  1155. status = acpi_bus_get_device(chandle, &child);
  1156. if (ACPI_FAILURE(status))
  1157. continue;
  1158. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1159. status = acpi_start_single_object(child);
  1160. if (ACPI_FAILURE(status))
  1161. continue;
  1162. }
  1163. /*
  1164. * If the device is present, enabled, and functioning then
  1165. * parse its scope (depth-first). Note that we need to
  1166. * represent absent devices to facilitate PnP notifications
  1167. * -- but only the subtree head (not all of its children,
  1168. * which will be enumerated when the parent is inserted).
  1169. *
  1170. * TBD: Need notifications and other detection mechanisms
  1171. * in place before we can fully implement this.
  1172. */
  1173. if (child->status.present) {
  1174. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1175. NULL, NULL);
  1176. if (ACPI_SUCCESS(status)) {
  1177. level++;
  1178. phandle = chandle;
  1179. chandle = NULL;
  1180. parent = child;
  1181. }
  1182. }
  1183. }
  1184. return 0;
  1185. }
  1186. int
  1187. acpi_bus_add(struct acpi_device **child,
  1188. struct acpi_device *parent, acpi_handle handle, int type)
  1189. {
  1190. int result;
  1191. struct acpi_bus_ops ops;
  1192. memset(&ops, 0, sizeof(ops));
  1193. ops.acpi_op_add = 1;
  1194. result = acpi_add_single_object(child, parent, handle, type, &ops);
  1195. if (!result)
  1196. result = acpi_bus_scan(*child, &ops);
  1197. return result;
  1198. }
  1199. EXPORT_SYMBOL(acpi_bus_add);
  1200. int acpi_bus_start(struct acpi_device *device)
  1201. {
  1202. int result;
  1203. struct acpi_bus_ops ops;
  1204. if (!device)
  1205. return -EINVAL;
  1206. result = acpi_start_single_object(device);
  1207. if (!result) {
  1208. memset(&ops, 0, sizeof(ops));
  1209. ops.acpi_op_start = 1;
  1210. result = acpi_bus_scan(device, &ops);
  1211. }
  1212. return result;
  1213. }
  1214. EXPORT_SYMBOL(acpi_bus_start);
  1215. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1216. {
  1217. acpi_status status;
  1218. struct acpi_device *parent, *child;
  1219. acpi_handle phandle, chandle;
  1220. acpi_object_type type;
  1221. u32 level = 1;
  1222. int err = 0;
  1223. parent = start;
  1224. phandle = start->handle;
  1225. child = chandle = NULL;
  1226. while ((level > 0) && parent && (!err)) {
  1227. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1228. chandle, &chandle);
  1229. /*
  1230. * If this scope is exhausted then move our way back up.
  1231. */
  1232. if (ACPI_FAILURE(status)) {
  1233. level--;
  1234. chandle = phandle;
  1235. acpi_get_parent(phandle, &phandle);
  1236. child = parent;
  1237. parent = parent->parent;
  1238. if (level == 0)
  1239. err = acpi_bus_remove(child, rmdevice);
  1240. else
  1241. err = acpi_bus_remove(child, 1);
  1242. continue;
  1243. }
  1244. status = acpi_get_type(chandle, &type);
  1245. if (ACPI_FAILURE(status)) {
  1246. continue;
  1247. }
  1248. /*
  1249. * If there is a device corresponding to chandle then
  1250. * parse it (depth-first).
  1251. */
  1252. if (acpi_bus_get_device(chandle, &child) == 0) {
  1253. level++;
  1254. phandle = chandle;
  1255. chandle = NULL;
  1256. parent = child;
  1257. }
  1258. continue;
  1259. }
  1260. return err;
  1261. }
  1262. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1263. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1264. {
  1265. int result = 0;
  1266. struct acpi_device *device = NULL;
  1267. struct acpi_bus_ops ops;
  1268. if (!root)
  1269. return -ENODEV;
  1270. memset(&ops, 0, sizeof(ops));
  1271. ops.acpi_op_add = 1;
  1272. ops.acpi_op_start = 1;
  1273. /*
  1274. * Enumerate all fixed-feature devices.
  1275. */
  1276. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1277. result = acpi_add_single_object(&device, acpi_root,
  1278. NULL,
  1279. ACPI_BUS_TYPE_POWER_BUTTON,
  1280. &ops);
  1281. }
  1282. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1283. result = acpi_add_single_object(&device, acpi_root,
  1284. NULL,
  1285. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1286. &ops);
  1287. }
  1288. return result;
  1289. }
  1290. int __init acpi_boot_ec_enable(void);
  1291. static int __init acpi_scan_init(void)
  1292. {
  1293. int result;
  1294. struct acpi_bus_ops ops;
  1295. if (acpi_disabled)
  1296. return 0;
  1297. memset(&ops, 0, sizeof(ops));
  1298. ops.acpi_op_add = 1;
  1299. ops.acpi_op_start = 1;
  1300. result = bus_register(&acpi_bus_type);
  1301. if (result) {
  1302. /* We don't want to quit even if we failed to add suspend/resume */
  1303. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1304. }
  1305. /*
  1306. * Create the root device in the bus's device tree
  1307. */
  1308. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1309. ACPI_BUS_TYPE_SYSTEM, &ops);
  1310. if (result)
  1311. goto Done;
  1312. /*
  1313. * Enumerate devices in the ACPI namespace.
  1314. */
  1315. result = acpi_bus_scan_fixed(acpi_root);
  1316. /* EC region might be needed at bus_scan, so enable it now */
  1317. acpi_boot_ec_enable();
  1318. if (!result)
  1319. result = acpi_bus_scan(acpi_root, &ops);
  1320. if (result)
  1321. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1322. Done:
  1323. return result;
  1324. }
  1325. subsys_initcall(acpi_scan_init);