scan.c 37 KB

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