scan.c 41 KB

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