scan.c 36 KB

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