scan.c 38 KB

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