scan.c 39 KB

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