scan.c 40 KB

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