scan.c 34 KB

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