scan.c 39 KB

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