scan.c 36 KB

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