scan.c 43 KB

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