scan.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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_ANY) {
  572. return AE_BAD_DATA;
  573. }
  574. device->wakeup.resources.handles[i] = element->reference.handle;
  575. }
  576. return AE_OK;
  577. }
  578. static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  579. {
  580. acpi_status status = 0;
  581. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  582. union acpi_object *package = NULL;
  583. union acpi_object in_arg[3];
  584. struct acpi_object_list arg_list = { 3, in_arg };
  585. acpi_status psw_status = AE_OK;
  586. struct acpi_device_id button_device_ids[] = {
  587. {"PNP0C0D", 0},
  588. {"PNP0C0C", 0},
  589. {"PNP0C0E", 0},
  590. {"", 0},
  591. };
  592. /* _PRW */
  593. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  594. if (ACPI_FAILURE(status)) {
  595. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  596. goto end;
  597. }
  598. package = (union acpi_object *)buffer.pointer;
  599. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  600. if (ACPI_FAILURE(status)) {
  601. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  602. goto end;
  603. }
  604. kfree(buffer.pointer);
  605. device->wakeup.flags.valid = 1;
  606. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  607. * system for the ACPI device with the _PRW object.
  608. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  609. * So it is necessary to call _DSW object first. Only when it is not
  610. * present will the _PSW object used.
  611. */
  612. /*
  613. * Three agruments are needed for the _DSW object.
  614. * Argument 0: enable/disable the wake capabilities
  615. * When _DSW object is called to disable the wake capabilities, maybe
  616. * the first argument is filled. The value of the other two agruments
  617. * is meaningless.
  618. */
  619. in_arg[0].type = ACPI_TYPE_INTEGER;
  620. in_arg[0].integer.value = 0;
  621. in_arg[1].type = ACPI_TYPE_INTEGER;
  622. in_arg[1].integer.value = 0;
  623. in_arg[2].type = ACPI_TYPE_INTEGER;
  624. in_arg[2].integer.value = 0;
  625. psw_status = acpi_evaluate_object(device->handle, "_DSW",
  626. &arg_list, NULL);
  627. if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
  628. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in evaluate _DSW\n"));
  629. /*
  630. * When the _DSW object is not present, OSPM will call _PSW object.
  631. */
  632. if (psw_status == AE_NOT_FOUND) {
  633. /*
  634. * Only one agruments is required for the _PSW object.
  635. * agrument 0: enable/disable the wake capabilities
  636. */
  637. arg_list.count = 1;
  638. in_arg[0].integer.value = 0;
  639. psw_status = acpi_evaluate_object(device->handle, "_PSW",
  640. &arg_list, NULL);
  641. if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
  642. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in "
  643. "evaluate _PSW\n"));
  644. }
  645. /* Power button, Lid switch always enable wakeup */
  646. if (!acpi_match_device_ids(device, button_device_ids))
  647. device->wakeup.flags.run_wake = 1;
  648. end:
  649. if (ACPI_FAILURE(status))
  650. device->flags.wake_capable = 0;
  651. return 0;
  652. }
  653. static int acpi_bus_get_power_flags(struct acpi_device *device)
  654. {
  655. acpi_status status = 0;
  656. acpi_handle handle = NULL;
  657. u32 i = 0;
  658. /*
  659. * Power Management Flags
  660. */
  661. status = acpi_get_handle(device->handle, "_PSC", &handle);
  662. if (ACPI_SUCCESS(status))
  663. device->power.flags.explicit_get = 1;
  664. status = acpi_get_handle(device->handle, "_IRC", &handle);
  665. if (ACPI_SUCCESS(status))
  666. device->power.flags.inrush_current = 1;
  667. /*
  668. * Enumerate supported power management states
  669. */
  670. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  671. struct acpi_device_power_state *ps = &device->power.states[i];
  672. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  673. /* Evaluate "_PRx" to se if power resources are referenced */
  674. acpi_evaluate_reference(device->handle, object_name, NULL,
  675. &ps->resources);
  676. if (ps->resources.count) {
  677. device->power.flags.power_resources = 1;
  678. ps->flags.valid = 1;
  679. }
  680. /* Evaluate "_PSx" to see if we can do explicit sets */
  681. object_name[2] = 'S';
  682. status = acpi_get_handle(device->handle, object_name, &handle);
  683. if (ACPI_SUCCESS(status)) {
  684. ps->flags.explicit_set = 1;
  685. ps->flags.valid = 1;
  686. }
  687. /* State is valid if we have some power control */
  688. if (ps->resources.count || ps->flags.explicit_set)
  689. ps->flags.valid = 1;
  690. ps->power = -1; /* Unknown - driver assigned */
  691. ps->latency = -1; /* Unknown - driver assigned */
  692. }
  693. /* Set defaults for D0 and D3 states (always valid) */
  694. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  695. device->power.states[ACPI_STATE_D0].power = 100;
  696. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  697. device->power.states[ACPI_STATE_D3].power = 0;
  698. /* TBD: System wake support and resource requirements. */
  699. device->power.state = ACPI_STATE_UNKNOWN;
  700. return 0;
  701. }
  702. static int acpi_bus_get_flags(struct acpi_device *device)
  703. {
  704. acpi_status status = AE_OK;
  705. acpi_handle temp = NULL;
  706. /* Presence of _STA indicates 'dynamic_status' */
  707. status = acpi_get_handle(device->handle, "_STA", &temp);
  708. if (ACPI_SUCCESS(status))
  709. device->flags.dynamic_status = 1;
  710. /* Presence of _CID indicates 'compatible_ids' */
  711. status = acpi_get_handle(device->handle, "_CID", &temp);
  712. if (ACPI_SUCCESS(status))
  713. device->flags.compatible_ids = 1;
  714. /* Presence of _RMV indicates 'removable' */
  715. status = acpi_get_handle(device->handle, "_RMV", &temp);
  716. if (ACPI_SUCCESS(status))
  717. device->flags.removable = 1;
  718. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  719. status = acpi_get_handle(device->handle, "_EJD", &temp);
  720. if (ACPI_SUCCESS(status))
  721. device->flags.ejectable = 1;
  722. else {
  723. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  724. if (ACPI_SUCCESS(status))
  725. device->flags.ejectable = 1;
  726. }
  727. /* Presence of _LCK indicates 'lockable' */
  728. status = acpi_get_handle(device->handle, "_LCK", &temp);
  729. if (ACPI_SUCCESS(status))
  730. device->flags.lockable = 1;
  731. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  732. status = acpi_get_handle(device->handle, "_PS0", &temp);
  733. if (ACPI_FAILURE(status))
  734. status = acpi_get_handle(device->handle, "_PR0", &temp);
  735. if (ACPI_SUCCESS(status))
  736. device->flags.power_manageable = 1;
  737. /* Presence of _PRW indicates wake capable */
  738. status = acpi_get_handle(device->handle, "_PRW", &temp);
  739. if (ACPI_SUCCESS(status))
  740. device->flags.wake_capable = 1;
  741. /* TBD: Performance management */
  742. return 0;
  743. }
  744. static void acpi_device_get_busid(struct acpi_device *device,
  745. acpi_handle handle, int type)
  746. {
  747. char bus_id[5] = { '?', 0 };
  748. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  749. int i = 0;
  750. /*
  751. * Bus ID
  752. * ------
  753. * The device's Bus ID is simply the object name.
  754. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  755. */
  756. switch (type) {
  757. case ACPI_BUS_TYPE_SYSTEM:
  758. strcpy(device->pnp.bus_id, "ACPI");
  759. break;
  760. case ACPI_BUS_TYPE_POWER_BUTTON:
  761. strcpy(device->pnp.bus_id, "PWRF");
  762. break;
  763. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  764. strcpy(device->pnp.bus_id, "SLPF");
  765. break;
  766. default:
  767. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  768. /* Clean up trailing underscores (if any) */
  769. for (i = 3; i > 1; i--) {
  770. if (bus_id[i] == '_')
  771. bus_id[i] = '\0';
  772. else
  773. break;
  774. }
  775. strcpy(device->pnp.bus_id, bus_id);
  776. break;
  777. }
  778. }
  779. static int
  780. acpi_video_bus_match(struct acpi_device *device)
  781. {
  782. acpi_handle h_dummy1;
  783. acpi_handle h_dummy2;
  784. acpi_handle h_dummy3;
  785. if (!device)
  786. return -EINVAL;
  787. /* Since there is no HID, CID for ACPI Video drivers, we have
  788. * to check well known required nodes for each feature we support.
  789. */
  790. /* Does this device able to support video switching ? */
  791. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  792. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  793. return 0;
  794. /* Does this device able to retrieve a video ROM ? */
  795. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  796. return 0;
  797. /* Does this device able to configure which video head to be POSTed ? */
  798. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  799. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  800. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  801. return 0;
  802. return -ENODEV;
  803. }
  804. /*
  805. * acpi_bay_match - see if a device is an ejectable driver bay
  806. *
  807. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  808. * then we can safely call it an ejectable drive bay
  809. */
  810. static int acpi_bay_match(struct acpi_device *device){
  811. acpi_status status;
  812. acpi_handle handle;
  813. acpi_handle tmp;
  814. acpi_handle phandle;
  815. handle = device->handle;
  816. status = acpi_get_handle(handle, "_EJ0", &tmp);
  817. if (ACPI_FAILURE(status))
  818. return -ENODEV;
  819. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  820. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  821. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  822. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  823. return 0;
  824. if (acpi_get_parent(handle, &phandle))
  825. return -ENODEV;
  826. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  827. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  828. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  829. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  830. return 0;
  831. return -ENODEV;
  832. }
  833. /*
  834. * acpi_dock_match - see if a device has a _DCK method
  835. */
  836. static int acpi_dock_match(struct acpi_device *device)
  837. {
  838. acpi_handle tmp;
  839. return acpi_get_handle(device->handle, "_DCK", &tmp);
  840. }
  841. static void acpi_device_set_id(struct acpi_device *device,
  842. struct acpi_device *parent, acpi_handle handle,
  843. int type)
  844. {
  845. struct acpi_device_info *info;
  846. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  847. char *hid = NULL;
  848. char *uid = NULL;
  849. struct acpi_compatible_id_list *cid_list = NULL;
  850. const char *cid_add = NULL;
  851. acpi_status status;
  852. switch (type) {
  853. case ACPI_BUS_TYPE_DEVICE:
  854. status = acpi_get_object_info(handle, &buffer);
  855. if (ACPI_FAILURE(status)) {
  856. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  857. return;
  858. }
  859. info = buffer.pointer;
  860. if (info->valid & ACPI_VALID_HID)
  861. hid = info->hardware_id.value;
  862. if (info->valid & ACPI_VALID_UID)
  863. uid = info->unique_id.value;
  864. if (info->valid & ACPI_VALID_CID)
  865. cid_list = &info->compatibility_id;
  866. if (info->valid & ACPI_VALID_ADR) {
  867. device->pnp.bus_address = info->address;
  868. device->flags.bus_address = 1;
  869. }
  870. /* If we have a video/bay/dock device, add our selfdefined
  871. HID to the CID list. Like that the video/bay/dock drivers
  872. will get autoloaded and the device might still match
  873. against another driver.
  874. */
  875. if (ACPI_SUCCESS(acpi_video_bus_match(device)))
  876. cid_add = ACPI_VIDEO_HID;
  877. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  878. cid_add = ACPI_BAY_HID;
  879. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  880. cid_add = ACPI_DOCK_HID;
  881. break;
  882. case ACPI_BUS_TYPE_POWER:
  883. hid = ACPI_POWER_HID;
  884. break;
  885. case ACPI_BUS_TYPE_PROCESSOR:
  886. hid = ACPI_PROCESSOR_HID;
  887. break;
  888. case ACPI_BUS_TYPE_SYSTEM:
  889. hid = ACPI_SYSTEM_HID;
  890. break;
  891. case ACPI_BUS_TYPE_THERMAL:
  892. hid = ACPI_THERMAL_HID;
  893. break;
  894. case ACPI_BUS_TYPE_POWER_BUTTON:
  895. hid = ACPI_BUTTON_HID_POWERF;
  896. break;
  897. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  898. hid = ACPI_BUTTON_HID_SLEEPF;
  899. break;
  900. }
  901. /*
  902. * \_SB
  903. * ----
  904. * Fix for the system root bus device -- the only root-level device.
  905. */
  906. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  907. hid = ACPI_BUS_HID;
  908. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  909. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  910. }
  911. if (hid) {
  912. strcpy(device->pnp.hardware_id, hid);
  913. device->flags.hardware_id = 1;
  914. }
  915. if (uid) {
  916. strcpy(device->pnp.unique_id, uid);
  917. device->flags.unique_id = 1;
  918. }
  919. if (cid_list || cid_add) {
  920. struct acpi_compatible_id_list *list;
  921. int size = 0;
  922. int count = 0;
  923. if (cid_list) {
  924. size = cid_list->size;
  925. } else if (cid_add) {
  926. size = sizeof(struct acpi_compatible_id_list);
  927. cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
  928. if (!cid_list) {
  929. printk(KERN_ERR "Memory allocation error\n");
  930. kfree(buffer.pointer);
  931. return;
  932. } else {
  933. cid_list->count = 0;
  934. cid_list->size = size;
  935. }
  936. }
  937. if (cid_add)
  938. size += sizeof(struct acpi_compatible_id);
  939. list = kmalloc(size, GFP_KERNEL);
  940. if (list) {
  941. if (cid_list) {
  942. memcpy(list, cid_list, cid_list->size);
  943. count = cid_list->count;
  944. }
  945. if (cid_add) {
  946. strncpy(list->id[count].value, cid_add,
  947. ACPI_MAX_CID_LENGTH);
  948. count++;
  949. device->flags.compatible_ids = 1;
  950. }
  951. list->size = size;
  952. list->count = count;
  953. device->pnp.cid_list = list;
  954. } else
  955. printk(KERN_ERR PREFIX "Memory allocation error\n");
  956. }
  957. kfree(buffer.pointer);
  958. }
  959. static int acpi_device_set_context(struct acpi_device *device, int type)
  960. {
  961. acpi_status status = AE_OK;
  962. int result = 0;
  963. /*
  964. * Context
  965. * -------
  966. * Attach this 'struct acpi_device' to the ACPI object. This makes
  967. * resolutions from handle->device very efficient. Note that we need
  968. * to be careful with fixed-feature devices as they all attach to the
  969. * root object.
  970. */
  971. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  972. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  973. status = acpi_attach_data(device->handle,
  974. acpi_bus_data_handler, device);
  975. if (ACPI_FAILURE(status)) {
  976. printk(KERN_ERR PREFIX "Error attaching device data\n");
  977. result = -ENODEV;
  978. }
  979. }
  980. return result;
  981. }
  982. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  983. {
  984. if (!dev)
  985. return -EINVAL;
  986. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  987. device_release_driver(&dev->dev);
  988. if (!rmdevice)
  989. return 0;
  990. /*
  991. * unbind _ADR-Based Devices when hot removal
  992. */
  993. if (dev->flags.bus_address) {
  994. if ((dev->parent) && (dev->parent->ops.unbind))
  995. dev->parent->ops.unbind(dev);
  996. }
  997. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  998. return 0;
  999. }
  1000. static int
  1001. acpi_is_child_device(struct acpi_device *device,
  1002. int (*matcher)(struct acpi_device *))
  1003. {
  1004. int result = -ENODEV;
  1005. do {
  1006. if (ACPI_SUCCESS(matcher(device)))
  1007. return AE_OK;
  1008. } while ((device = device->parent));
  1009. return result;
  1010. }
  1011. static int
  1012. acpi_add_single_object(struct acpi_device **child,
  1013. struct acpi_device *parent, acpi_handle handle, int type,
  1014. struct acpi_bus_ops *ops)
  1015. {
  1016. int result = 0;
  1017. struct acpi_device *device = NULL;
  1018. if (!child)
  1019. return -EINVAL;
  1020. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1021. if (!device) {
  1022. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1023. return -ENOMEM;
  1024. }
  1025. device->handle = handle;
  1026. device->parent = parent;
  1027. device->bus_ops = *ops; /* workround for not call .start */
  1028. acpi_device_get_busid(device, handle, type);
  1029. /*
  1030. * Flags
  1031. * -----
  1032. * Get prior to calling acpi_bus_get_status() so we know whether
  1033. * or not _STA is present. Note that we only look for object
  1034. * handles -- cannot evaluate objects until we know the device is
  1035. * present and properly initialized.
  1036. */
  1037. result = acpi_bus_get_flags(device);
  1038. if (result)
  1039. goto end;
  1040. /*
  1041. * Status
  1042. * ------
  1043. * See if the device is present. We always assume that non-Device
  1044. * and non-Processor objects (e.g. thermal zones, power resources,
  1045. * etc.) are present, functioning, etc. (at least when parent object
  1046. * is present). Note that _STA has a different meaning for some
  1047. * objects (e.g. power resources) so we need to be careful how we use
  1048. * it.
  1049. */
  1050. switch (type) {
  1051. case ACPI_BUS_TYPE_PROCESSOR:
  1052. case ACPI_BUS_TYPE_DEVICE:
  1053. result = acpi_bus_get_status(device);
  1054. if (ACPI_FAILURE(result)) {
  1055. result = -ENODEV;
  1056. goto end;
  1057. }
  1058. if (!device->status.present) {
  1059. /* Bay and dock should be handled even if absent */
  1060. if (!ACPI_SUCCESS(
  1061. acpi_is_child_device(device, acpi_bay_match)) &&
  1062. !ACPI_SUCCESS(
  1063. acpi_is_child_device(device, acpi_dock_match))) {
  1064. result = -ENODEV;
  1065. goto end;
  1066. }
  1067. }
  1068. break;
  1069. default:
  1070. STRUCT_TO_INT(device->status) =
  1071. ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  1072. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  1073. break;
  1074. }
  1075. /*
  1076. * Initialize Device
  1077. * -----------------
  1078. * TBD: Synch with Core's enumeration/initialization process.
  1079. */
  1080. /*
  1081. * Hardware ID, Unique ID, & Bus Address
  1082. * -------------------------------------
  1083. */
  1084. acpi_device_set_id(device, parent, handle, type);
  1085. /*
  1086. * Power Management
  1087. * ----------------
  1088. */
  1089. if (device->flags.power_manageable) {
  1090. result = acpi_bus_get_power_flags(device);
  1091. if (result)
  1092. goto end;
  1093. }
  1094. /*
  1095. * Wakeup device management
  1096. *-----------------------
  1097. */
  1098. if (device->flags.wake_capable) {
  1099. result = acpi_bus_get_wakeup_device_flags(device);
  1100. if (result)
  1101. goto end;
  1102. }
  1103. /*
  1104. * Performance Management
  1105. * ----------------------
  1106. */
  1107. if (device->flags.performance_manageable) {
  1108. result = acpi_bus_get_perf_flags(device);
  1109. if (result)
  1110. goto end;
  1111. }
  1112. if ((result = acpi_device_set_context(device, type)))
  1113. goto end;
  1114. result = acpi_device_register(device, parent);
  1115. /*
  1116. * Bind _ADR-Based Devices when hot add
  1117. */
  1118. if (device->flags.bus_address) {
  1119. if (device->parent && device->parent->ops.bind)
  1120. device->parent->ops.bind(device);
  1121. }
  1122. end:
  1123. if (!result)
  1124. *child = device;
  1125. else {
  1126. kfree(device->pnp.cid_list);
  1127. kfree(device);
  1128. }
  1129. return result;
  1130. }
  1131. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  1132. {
  1133. acpi_status status = AE_OK;
  1134. struct acpi_device *parent = NULL;
  1135. struct acpi_device *child = NULL;
  1136. acpi_handle phandle = NULL;
  1137. acpi_handle chandle = NULL;
  1138. acpi_object_type type = 0;
  1139. u32 level = 1;
  1140. if (!start)
  1141. return -EINVAL;
  1142. parent = start;
  1143. phandle = start->handle;
  1144. /*
  1145. * Parse through the ACPI namespace, identify all 'devices', and
  1146. * create a new 'struct acpi_device' for each.
  1147. */
  1148. while ((level > 0) && parent) {
  1149. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1150. chandle, &chandle);
  1151. /*
  1152. * If this scope is exhausted then move our way back up.
  1153. */
  1154. if (ACPI_FAILURE(status)) {
  1155. level--;
  1156. chandle = phandle;
  1157. acpi_get_parent(phandle, &phandle);
  1158. if (parent->parent)
  1159. parent = parent->parent;
  1160. continue;
  1161. }
  1162. status = acpi_get_type(chandle, &type);
  1163. if (ACPI_FAILURE(status))
  1164. continue;
  1165. /*
  1166. * If this is a scope object then parse it (depth-first).
  1167. */
  1168. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  1169. level++;
  1170. phandle = chandle;
  1171. chandle = NULL;
  1172. continue;
  1173. }
  1174. /*
  1175. * We're only interested in objects that we consider 'devices'.
  1176. */
  1177. switch (type) {
  1178. case ACPI_TYPE_DEVICE:
  1179. type = ACPI_BUS_TYPE_DEVICE;
  1180. break;
  1181. case ACPI_TYPE_PROCESSOR:
  1182. type = ACPI_BUS_TYPE_PROCESSOR;
  1183. break;
  1184. case ACPI_TYPE_THERMAL:
  1185. type = ACPI_BUS_TYPE_THERMAL;
  1186. break;
  1187. case ACPI_TYPE_POWER:
  1188. type = ACPI_BUS_TYPE_POWER;
  1189. break;
  1190. default:
  1191. continue;
  1192. }
  1193. if (ops->acpi_op_add)
  1194. status = acpi_add_single_object(&child, parent,
  1195. chandle, type, ops);
  1196. else
  1197. status = acpi_bus_get_device(chandle, &child);
  1198. if (ACPI_FAILURE(status))
  1199. continue;
  1200. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  1201. status = acpi_start_single_object(child);
  1202. if (ACPI_FAILURE(status))
  1203. continue;
  1204. }
  1205. /*
  1206. * If the device is present, enabled, and functioning then
  1207. * parse its scope (depth-first). Note that we need to
  1208. * represent absent devices to facilitate PnP notifications
  1209. * -- but only the subtree head (not all of its children,
  1210. * which will be enumerated when the parent is inserted).
  1211. *
  1212. * TBD: Need notifications and other detection mechanisms
  1213. * in place before we can fully implement this.
  1214. */
  1215. if (child->status.present) {
  1216. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  1217. NULL, NULL);
  1218. if (ACPI_SUCCESS(status)) {
  1219. level++;
  1220. phandle = chandle;
  1221. chandle = NULL;
  1222. parent = child;
  1223. }
  1224. }
  1225. }
  1226. return 0;
  1227. }
  1228. int
  1229. acpi_bus_add(struct acpi_device **child,
  1230. struct acpi_device *parent, acpi_handle handle, int type)
  1231. {
  1232. int result;
  1233. struct acpi_bus_ops ops;
  1234. memset(&ops, 0, sizeof(ops));
  1235. ops.acpi_op_add = 1;
  1236. result = acpi_add_single_object(child, parent, handle, type, &ops);
  1237. if (!result)
  1238. result = acpi_bus_scan(*child, &ops);
  1239. return result;
  1240. }
  1241. EXPORT_SYMBOL(acpi_bus_add);
  1242. int acpi_bus_start(struct acpi_device *device)
  1243. {
  1244. int result;
  1245. struct acpi_bus_ops ops;
  1246. if (!device)
  1247. return -EINVAL;
  1248. result = acpi_start_single_object(device);
  1249. if (!result) {
  1250. memset(&ops, 0, sizeof(ops));
  1251. ops.acpi_op_start = 1;
  1252. result = acpi_bus_scan(device, &ops);
  1253. }
  1254. return result;
  1255. }
  1256. EXPORT_SYMBOL(acpi_bus_start);
  1257. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1258. {
  1259. acpi_status status;
  1260. struct acpi_device *parent, *child;
  1261. acpi_handle phandle, chandle;
  1262. acpi_object_type type;
  1263. u32 level = 1;
  1264. int err = 0;
  1265. parent = start;
  1266. phandle = start->handle;
  1267. child = chandle = NULL;
  1268. while ((level > 0) && parent && (!err)) {
  1269. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1270. chandle, &chandle);
  1271. /*
  1272. * If this scope is exhausted then move our way back up.
  1273. */
  1274. if (ACPI_FAILURE(status)) {
  1275. level--;
  1276. chandle = phandle;
  1277. acpi_get_parent(phandle, &phandle);
  1278. child = parent;
  1279. parent = parent->parent;
  1280. if (level == 0)
  1281. err = acpi_bus_remove(child, rmdevice);
  1282. else
  1283. err = acpi_bus_remove(child, 1);
  1284. continue;
  1285. }
  1286. status = acpi_get_type(chandle, &type);
  1287. if (ACPI_FAILURE(status)) {
  1288. continue;
  1289. }
  1290. /*
  1291. * If there is a device corresponding to chandle then
  1292. * parse it (depth-first).
  1293. */
  1294. if (acpi_bus_get_device(chandle, &child) == 0) {
  1295. level++;
  1296. phandle = chandle;
  1297. chandle = NULL;
  1298. parent = child;
  1299. }
  1300. continue;
  1301. }
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1305. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1306. {
  1307. int result = 0;
  1308. struct acpi_device *device = NULL;
  1309. struct acpi_bus_ops ops;
  1310. if (!root)
  1311. return -ENODEV;
  1312. memset(&ops, 0, sizeof(ops));
  1313. ops.acpi_op_add = 1;
  1314. ops.acpi_op_start = 1;
  1315. /*
  1316. * Enumerate all fixed-feature devices.
  1317. */
  1318. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1319. result = acpi_add_single_object(&device, acpi_root,
  1320. NULL,
  1321. ACPI_BUS_TYPE_POWER_BUTTON,
  1322. &ops);
  1323. }
  1324. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1325. result = acpi_add_single_object(&device, acpi_root,
  1326. NULL,
  1327. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1328. &ops);
  1329. }
  1330. return result;
  1331. }
  1332. int __init acpi_boot_ec_enable(void);
  1333. static int __init acpi_scan_init(void)
  1334. {
  1335. int result;
  1336. struct acpi_bus_ops ops;
  1337. if (acpi_disabled)
  1338. return 0;
  1339. memset(&ops, 0, sizeof(ops));
  1340. ops.acpi_op_add = 1;
  1341. ops.acpi_op_start = 1;
  1342. result = bus_register(&acpi_bus_type);
  1343. if (result) {
  1344. /* We don't want to quit even if we failed to add suspend/resume */
  1345. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1346. }
  1347. /*
  1348. * Create the root device in the bus's device tree
  1349. */
  1350. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1351. ACPI_BUS_TYPE_SYSTEM, &ops);
  1352. if (result)
  1353. goto Done;
  1354. /*
  1355. * Enumerate devices in the ACPI namespace.
  1356. */
  1357. result = acpi_bus_scan_fixed(acpi_root);
  1358. /* EC region might be needed at bus_scan, so enable it now */
  1359. acpi_boot_ec_enable();
  1360. if (!result)
  1361. result = acpi_bus_scan(acpi_root, &ops);
  1362. if (result)
  1363. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1364. Done:
  1365. return result;
  1366. }
  1367. subsys_initcall(acpi_scan_init);