scan.c 40 KB

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