scan.c 40 KB

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