scan.c 36 KB

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