scan.c 38 KB

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