scan.c 45 KB

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