scan.c 38 KB

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