scan.c 40 KB

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