scan.c 41 KB

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