scan.c 36 KB

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