scan.c 41 KB

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