scan.c 38 KB

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