scan.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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_dev->add_type >= ACPI_BUS_ADD_MATCH
  418. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  419. }
  420. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  421. {
  422. struct acpi_device *acpi_dev = to_acpi_device(dev);
  423. int len;
  424. if (list_empty(&acpi_dev->pnp.ids))
  425. return 0;
  426. if (add_uevent_var(env, "MODALIAS="))
  427. return -ENOMEM;
  428. len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
  429. sizeof(env->buf) - env->buflen);
  430. if (len >= (sizeof(env->buf) - env->buflen))
  431. return -ENOMEM;
  432. env->buflen += len;
  433. return 0;
  434. }
  435. static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
  436. {
  437. struct acpi_device *device = data;
  438. device->driver->ops.notify(device, event);
  439. }
  440. static acpi_status acpi_device_notify_fixed(void *data)
  441. {
  442. struct acpi_device *device = data;
  443. /* Fixed hardware devices have no handles */
  444. acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
  445. return AE_OK;
  446. }
  447. static int acpi_device_install_notify_handler(struct acpi_device *device)
  448. {
  449. acpi_status status;
  450. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  451. status =
  452. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  453. acpi_device_notify_fixed,
  454. device);
  455. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  456. status =
  457. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  458. acpi_device_notify_fixed,
  459. device);
  460. else
  461. status = acpi_install_notify_handler(device->handle,
  462. ACPI_DEVICE_NOTIFY,
  463. acpi_device_notify,
  464. device);
  465. if (ACPI_FAILURE(status))
  466. return -EINVAL;
  467. return 0;
  468. }
  469. static void acpi_device_remove_notify_handler(struct acpi_device *device)
  470. {
  471. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  472. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  473. acpi_device_notify_fixed);
  474. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  475. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  476. acpi_device_notify_fixed);
  477. else
  478. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  479. acpi_device_notify);
  480. }
  481. static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
  482. static int acpi_start_single_object(struct acpi_device *);
  483. static int acpi_device_probe(struct device * dev)
  484. {
  485. struct acpi_device *acpi_dev = to_acpi_device(dev);
  486. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  487. int ret;
  488. ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
  489. if (!ret) {
  490. if (acpi_dev->add_type == ACPI_BUS_ADD_START)
  491. acpi_start_single_object(acpi_dev);
  492. if (acpi_drv->ops.notify) {
  493. ret = acpi_device_install_notify_handler(acpi_dev);
  494. if (ret) {
  495. if (acpi_drv->ops.remove)
  496. acpi_drv->ops.remove(acpi_dev,
  497. acpi_dev->removal_type);
  498. return ret;
  499. }
  500. }
  501. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  502. "Found driver [%s] for device [%s]\n",
  503. acpi_drv->name, acpi_dev->pnp.bus_id));
  504. get_device(dev);
  505. }
  506. return ret;
  507. }
  508. static int acpi_device_remove(struct device * dev)
  509. {
  510. struct acpi_device *acpi_dev = to_acpi_device(dev);
  511. struct acpi_driver *acpi_drv = acpi_dev->driver;
  512. if (acpi_drv) {
  513. if (acpi_drv->ops.notify)
  514. acpi_device_remove_notify_handler(acpi_dev);
  515. if (acpi_drv->ops.remove)
  516. acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
  517. }
  518. acpi_dev->driver = NULL;
  519. acpi_dev->driver_data = NULL;
  520. put_device(dev);
  521. return 0;
  522. }
  523. struct bus_type acpi_bus_type = {
  524. .name = "acpi",
  525. .match = acpi_bus_match,
  526. .probe = acpi_device_probe,
  527. .remove = acpi_device_remove,
  528. .uevent = acpi_device_uevent,
  529. };
  530. static int acpi_device_register(struct acpi_device *device)
  531. {
  532. int result;
  533. struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
  534. int found = 0;
  535. /*
  536. * Linkage
  537. * -------
  538. * Link this device to its parent and siblings.
  539. */
  540. INIT_LIST_HEAD(&device->children);
  541. INIT_LIST_HEAD(&device->node);
  542. INIT_LIST_HEAD(&device->wakeup_list);
  543. INIT_LIST_HEAD(&device->physical_node_list);
  544. mutex_init(&device->physical_node_lock);
  545. new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
  546. if (!new_bus_id) {
  547. printk(KERN_ERR PREFIX "Memory allocation error\n");
  548. return -ENOMEM;
  549. }
  550. mutex_lock(&acpi_device_lock);
  551. /*
  552. * Find suitable bus_id and instance number in acpi_bus_id_list
  553. * If failed, create one and link it into acpi_bus_id_list
  554. */
  555. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
  556. if (!strcmp(acpi_device_bus_id->bus_id,
  557. acpi_device_hid(device))) {
  558. acpi_device_bus_id->instance_no++;
  559. found = 1;
  560. kfree(new_bus_id);
  561. break;
  562. }
  563. }
  564. if (!found) {
  565. acpi_device_bus_id = new_bus_id;
  566. strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
  567. acpi_device_bus_id->instance_no = 0;
  568. list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
  569. }
  570. dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
  571. if (device->parent)
  572. list_add_tail(&device->node, &device->parent->children);
  573. if (device->wakeup.flags.valid)
  574. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  575. mutex_unlock(&acpi_device_lock);
  576. if (device->parent)
  577. device->dev.parent = &device->parent->dev;
  578. device->dev.bus = &acpi_bus_type;
  579. device->dev.release = &acpi_device_release;
  580. result = device_register(&device->dev);
  581. if (result) {
  582. dev_err(&device->dev, "Error registering device\n");
  583. goto end;
  584. }
  585. result = acpi_device_setup_files(device);
  586. if (result)
  587. printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
  588. dev_name(&device->dev));
  589. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  590. return 0;
  591. end:
  592. mutex_lock(&acpi_device_lock);
  593. if (device->parent)
  594. list_del(&device->node);
  595. list_del(&device->wakeup_list);
  596. mutex_unlock(&acpi_device_lock);
  597. return result;
  598. }
  599. static void acpi_device_unregister(struct acpi_device *device, int type)
  600. {
  601. mutex_lock(&acpi_device_lock);
  602. if (device->parent)
  603. list_del(&device->node);
  604. list_del(&device->wakeup_list);
  605. mutex_unlock(&acpi_device_lock);
  606. acpi_detach_data(device->handle, acpi_bus_data_handler);
  607. acpi_device_remove_files(device);
  608. device_unregister(&device->dev);
  609. }
  610. /* --------------------------------------------------------------------------
  611. Driver Management
  612. -------------------------------------------------------------------------- */
  613. /**
  614. * acpi_bus_driver_init - add a device to a driver
  615. * @device: the device to add and initialize
  616. * @driver: driver for the device
  617. *
  618. * Used to initialize a device via its device driver. Called whenever a
  619. * driver is bound to a device. Invokes the driver's add() ops.
  620. */
  621. static int
  622. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  623. {
  624. int result = 0;
  625. if (!device || !driver)
  626. return -EINVAL;
  627. if (!driver->ops.add)
  628. return -ENOSYS;
  629. result = driver->ops.add(device);
  630. if (result) {
  631. device->driver = NULL;
  632. device->driver_data = NULL;
  633. return result;
  634. }
  635. device->driver = driver;
  636. /*
  637. * TBD - Configuration Management: Assign resources to device based
  638. * upon possible configuration and currently allocated resources.
  639. */
  640. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  641. "Driver successfully bound to device\n"));
  642. return 0;
  643. }
  644. static int acpi_start_single_object(struct acpi_device *device)
  645. {
  646. int result = 0;
  647. struct acpi_driver *driver;
  648. if (!(driver = device->driver))
  649. return 0;
  650. if (driver->ops.start) {
  651. result = driver->ops.start(device);
  652. if (result && driver->ops.remove)
  653. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  654. }
  655. return result;
  656. }
  657. /**
  658. * acpi_bus_register_driver - register a driver with the ACPI bus
  659. * @driver: driver being registered
  660. *
  661. * Registers a driver with the ACPI bus. Searches the namespace for all
  662. * devices that match the driver's criteria and binds. Returns zero for
  663. * success or a negative error status for failure.
  664. */
  665. int acpi_bus_register_driver(struct acpi_driver *driver)
  666. {
  667. int ret;
  668. if (acpi_disabled)
  669. return -ENODEV;
  670. driver->drv.name = driver->name;
  671. driver->drv.bus = &acpi_bus_type;
  672. driver->drv.owner = driver->owner;
  673. ret = driver_register(&driver->drv);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL(acpi_bus_register_driver);
  677. /**
  678. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  679. * @driver: driver to unregister
  680. *
  681. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  682. * devices that match the driver's criteria and unbinds.
  683. */
  684. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  685. {
  686. driver_unregister(&driver->drv);
  687. }
  688. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  689. /* --------------------------------------------------------------------------
  690. Device Enumeration
  691. -------------------------------------------------------------------------- */
  692. static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
  693. {
  694. acpi_status status;
  695. int ret;
  696. struct acpi_device *device;
  697. /*
  698. * Fixed hardware devices do not appear in the namespace and do not
  699. * have handles, but we fabricate acpi_devices for them, so we have
  700. * to deal with them specially.
  701. */
  702. if (handle == NULL)
  703. return acpi_root;
  704. do {
  705. status = acpi_get_parent(handle, &handle);
  706. if (status == AE_NULL_ENTRY)
  707. return NULL;
  708. if (ACPI_FAILURE(status))
  709. return acpi_root;
  710. ret = acpi_bus_get_device(handle, &device);
  711. if (ret == 0)
  712. return device;
  713. } while (1);
  714. }
  715. acpi_status
  716. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  717. {
  718. acpi_status status;
  719. acpi_handle tmp;
  720. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  721. union acpi_object *obj;
  722. status = acpi_get_handle(handle, "_EJD", &tmp);
  723. if (ACPI_FAILURE(status))
  724. return status;
  725. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  726. if (ACPI_SUCCESS(status)) {
  727. obj = buffer.pointer;
  728. status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
  729. ejd);
  730. kfree(buffer.pointer);
  731. }
  732. return status;
  733. }
  734. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  735. void acpi_bus_data_handler(acpi_handle handle, void *context)
  736. {
  737. /* TBD */
  738. return;
  739. }
  740. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  741. {
  742. device->performance.state = ACPI_STATE_UNKNOWN;
  743. return 0;
  744. }
  745. static acpi_status
  746. acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
  747. struct acpi_device_wakeup *wakeup)
  748. {
  749. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  750. union acpi_object *package = NULL;
  751. union acpi_object *element = NULL;
  752. acpi_status status;
  753. int i = 0;
  754. if (!wakeup)
  755. return AE_BAD_PARAMETER;
  756. /* _PRW */
  757. status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
  758. if (ACPI_FAILURE(status)) {
  759. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  760. return status;
  761. }
  762. package = (union acpi_object *)buffer.pointer;
  763. if (!package || (package->package.count < 2)) {
  764. status = AE_BAD_DATA;
  765. goto out;
  766. }
  767. element = &(package->package.elements[0]);
  768. if (!element) {
  769. status = AE_BAD_DATA;
  770. goto out;
  771. }
  772. if (element->type == ACPI_TYPE_PACKAGE) {
  773. if ((element->package.count < 2) ||
  774. (element->package.elements[0].type !=
  775. ACPI_TYPE_LOCAL_REFERENCE)
  776. || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
  777. status = AE_BAD_DATA;
  778. goto out;
  779. }
  780. wakeup->gpe_device =
  781. element->package.elements[0].reference.handle;
  782. wakeup->gpe_number =
  783. (u32) element->package.elements[1].integer.value;
  784. } else if (element->type == ACPI_TYPE_INTEGER) {
  785. wakeup->gpe_device = NULL;
  786. wakeup->gpe_number = element->integer.value;
  787. } else {
  788. status = AE_BAD_DATA;
  789. goto out;
  790. }
  791. element = &(package->package.elements[1]);
  792. if (element->type != ACPI_TYPE_INTEGER) {
  793. status = AE_BAD_DATA;
  794. goto out;
  795. }
  796. wakeup->sleep_state = element->integer.value;
  797. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  798. status = AE_NO_MEMORY;
  799. goto out;
  800. }
  801. wakeup->resources.count = package->package.count - 2;
  802. for (i = 0; i < wakeup->resources.count; i++) {
  803. element = &(package->package.elements[i + 2]);
  804. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  805. status = AE_BAD_DATA;
  806. goto out;
  807. }
  808. wakeup->resources.handles[i] = element->reference.handle;
  809. }
  810. acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
  811. out:
  812. kfree(buffer.pointer);
  813. return status;
  814. }
  815. static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
  816. {
  817. struct acpi_device_id button_device_ids[] = {
  818. {"PNP0C0C", 0},
  819. {"PNP0C0D", 0},
  820. {"PNP0C0E", 0},
  821. {"", 0},
  822. };
  823. acpi_status status;
  824. acpi_event_status event_status;
  825. device->wakeup.flags.notifier_present = 0;
  826. /* Power button, Lid switch always enable wakeup */
  827. if (!acpi_match_device_ids(device, button_device_ids)) {
  828. device->wakeup.flags.run_wake = 1;
  829. if (!acpi_match_device_ids(device, &button_device_ids[1])) {
  830. /* Do not use Lid/sleep button for S5 wakeup */
  831. if (device->wakeup.sleep_state == ACPI_STATE_S5)
  832. device->wakeup.sleep_state = ACPI_STATE_S4;
  833. }
  834. device_set_wakeup_capable(&device->dev, true);
  835. return;
  836. }
  837. status = acpi_get_gpe_status(device->wakeup.gpe_device,
  838. device->wakeup.gpe_number,
  839. &event_status);
  840. if (status == AE_OK)
  841. device->wakeup.flags.run_wake =
  842. !!(event_status & ACPI_EVENT_FLAG_HANDLE);
  843. }
  844. static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  845. {
  846. acpi_handle temp;
  847. acpi_status status = 0;
  848. int psw_error;
  849. /* Presence of _PRW indicates wake capable */
  850. status = acpi_get_handle(device->handle, "_PRW", &temp);
  851. if (ACPI_FAILURE(status))
  852. return;
  853. status = acpi_bus_extract_wakeup_device_power_package(device->handle,
  854. &device->wakeup);
  855. if (ACPI_FAILURE(status)) {
  856. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  857. return;
  858. }
  859. device->wakeup.flags.valid = 1;
  860. device->wakeup.prepare_count = 0;
  861. acpi_bus_set_run_wake_flags(device);
  862. /* Call _PSW/_DSW object to disable its ability to wake the sleeping
  863. * system for the ACPI device with the _PRW object.
  864. * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
  865. * So it is necessary to call _DSW object first. Only when it is not
  866. * present will the _PSW object used.
  867. */
  868. psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
  869. if (psw_error)
  870. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  871. "error in _DSW or _PSW evaluation\n"));
  872. }
  873. static void acpi_bus_add_power_resource(acpi_handle handle);
  874. static int acpi_bus_get_power_flags(struct acpi_device *device)
  875. {
  876. acpi_status status = 0;
  877. acpi_handle handle = NULL;
  878. u32 i = 0;
  879. /*
  880. * Power Management Flags
  881. */
  882. status = acpi_get_handle(device->handle, "_PSC", &handle);
  883. if (ACPI_SUCCESS(status))
  884. device->power.flags.explicit_get = 1;
  885. status = acpi_get_handle(device->handle, "_IRC", &handle);
  886. if (ACPI_SUCCESS(status))
  887. device->power.flags.inrush_current = 1;
  888. /*
  889. * Enumerate supported power management states
  890. */
  891. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  892. struct acpi_device_power_state *ps = &device->power.states[i];
  893. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  894. /* Evaluate "_PRx" to se if power resources are referenced */
  895. acpi_evaluate_reference(device->handle, object_name, NULL,
  896. &ps->resources);
  897. if (ps->resources.count) {
  898. int j;
  899. device->power.flags.power_resources = 1;
  900. for (j = 0; j < ps->resources.count; j++)
  901. acpi_bus_add_power_resource(ps->resources.handles[j]);
  902. }
  903. /* Evaluate "_PSx" to see if we can do explicit sets */
  904. object_name[2] = 'S';
  905. status = acpi_get_handle(device->handle, object_name, &handle);
  906. if (ACPI_SUCCESS(status))
  907. ps->flags.explicit_set = 1;
  908. /*
  909. * State is valid if there are means to put the device into it.
  910. * D3hot is only valid if _PR3 present.
  911. */
  912. if (ps->resources.count ||
  913. (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
  914. ps->flags.valid = 1;
  915. ps->flags.os_accessible = 1;
  916. }
  917. ps->power = -1; /* Unknown - driver assigned */
  918. ps->latency = -1; /* Unknown - driver assigned */
  919. }
  920. /* Set defaults for D0 and D3 states (always valid) */
  921. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  922. device->power.states[ACPI_STATE_D0].power = 100;
  923. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  924. device->power.states[ACPI_STATE_D3].power = 0;
  925. /* Set D3cold's explicit_set flag if _PS3 exists. */
  926. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
  927. device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
  928. /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
  929. if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
  930. device->power.flags.power_resources)
  931. device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
  932. acpi_bus_init_power(device);
  933. return 0;
  934. }
  935. static int acpi_bus_get_flags(struct acpi_device *device)
  936. {
  937. acpi_status status = AE_OK;
  938. acpi_handle temp = NULL;
  939. /* Presence of _STA indicates 'dynamic_status' */
  940. status = acpi_get_handle(device->handle, "_STA", &temp);
  941. if (ACPI_SUCCESS(status))
  942. device->flags.dynamic_status = 1;
  943. /* Presence of _RMV indicates 'removable' */
  944. status = acpi_get_handle(device->handle, "_RMV", &temp);
  945. if (ACPI_SUCCESS(status))
  946. device->flags.removable = 1;
  947. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  948. status = acpi_get_handle(device->handle, "_EJD", &temp);
  949. if (ACPI_SUCCESS(status))
  950. device->flags.ejectable = 1;
  951. else {
  952. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  953. if (ACPI_SUCCESS(status))
  954. device->flags.ejectable = 1;
  955. }
  956. /* Power resources cannot be power manageable. */
  957. if (device->device_type == ACPI_BUS_TYPE_POWER)
  958. return 0;
  959. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  960. status = acpi_get_handle(device->handle, "_PS0", &temp);
  961. if (ACPI_FAILURE(status))
  962. status = acpi_get_handle(device->handle, "_PR0", &temp);
  963. if (ACPI_SUCCESS(status))
  964. device->flags.power_manageable = 1;
  965. /* TBD: Performance management */
  966. return 0;
  967. }
  968. static void acpi_device_get_busid(struct acpi_device *device)
  969. {
  970. char bus_id[5] = { '?', 0 };
  971. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  972. int i = 0;
  973. /*
  974. * Bus ID
  975. * ------
  976. * The device's Bus ID is simply the object name.
  977. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  978. */
  979. if (ACPI_IS_ROOT_DEVICE(device)) {
  980. strcpy(device->pnp.bus_id, "ACPI");
  981. return;
  982. }
  983. switch (device->device_type) {
  984. case ACPI_BUS_TYPE_POWER_BUTTON:
  985. strcpy(device->pnp.bus_id, "PWRF");
  986. break;
  987. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  988. strcpy(device->pnp.bus_id, "SLPF");
  989. break;
  990. default:
  991. acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
  992. /* Clean up trailing underscores (if any) */
  993. for (i = 3; i > 1; i--) {
  994. if (bus_id[i] == '_')
  995. bus_id[i] = '\0';
  996. else
  997. break;
  998. }
  999. strcpy(device->pnp.bus_id, bus_id);
  1000. break;
  1001. }
  1002. }
  1003. /*
  1004. * acpi_bay_match - see if a device is an ejectable driver bay
  1005. *
  1006. * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
  1007. * then we can safely call it an ejectable drive bay
  1008. */
  1009. static int acpi_bay_match(struct acpi_device *device){
  1010. acpi_status status;
  1011. acpi_handle handle;
  1012. acpi_handle tmp;
  1013. acpi_handle phandle;
  1014. handle = device->handle;
  1015. status = acpi_get_handle(handle, "_EJ0", &tmp);
  1016. if (ACPI_FAILURE(status))
  1017. return -ENODEV;
  1018. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  1019. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  1020. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  1021. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  1022. return 0;
  1023. if (acpi_get_parent(handle, &phandle))
  1024. return -ENODEV;
  1025. if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
  1026. (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
  1027. (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
  1028. (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
  1029. return 0;
  1030. return -ENODEV;
  1031. }
  1032. /*
  1033. * acpi_dock_match - see if a device has a _DCK method
  1034. */
  1035. static int acpi_dock_match(struct acpi_device *device)
  1036. {
  1037. acpi_handle tmp;
  1038. return acpi_get_handle(device->handle, "_DCK", &tmp);
  1039. }
  1040. const char *acpi_device_hid(struct acpi_device *device)
  1041. {
  1042. struct acpi_hardware_id *hid;
  1043. if (list_empty(&device->pnp.ids))
  1044. return dummy_hid;
  1045. hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
  1046. return hid->id;
  1047. }
  1048. EXPORT_SYMBOL(acpi_device_hid);
  1049. static void acpi_add_id(struct acpi_device *device, const char *dev_id)
  1050. {
  1051. struct acpi_hardware_id *id;
  1052. id = kmalloc(sizeof(*id), GFP_KERNEL);
  1053. if (!id)
  1054. return;
  1055. id->id = kstrdup(dev_id, GFP_KERNEL);
  1056. if (!id->id) {
  1057. kfree(id);
  1058. return;
  1059. }
  1060. list_add_tail(&id->list, &device->pnp.ids);
  1061. }
  1062. /*
  1063. * Old IBM workstations have a DSDT bug wherein the SMBus object
  1064. * lacks the SMBUS01 HID and the methods do not have the necessary "_"
  1065. * prefix. Work around this.
  1066. */
  1067. static int acpi_ibm_smbus_match(struct acpi_device *device)
  1068. {
  1069. acpi_handle h_dummy;
  1070. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  1071. int result;
  1072. if (!dmi_name_in_vendors("IBM"))
  1073. return -ENODEV;
  1074. /* Look for SMBS object */
  1075. result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
  1076. if (result)
  1077. return result;
  1078. if (strcmp("SMBS", path.pointer)) {
  1079. result = -ENODEV;
  1080. goto out;
  1081. }
  1082. /* Does it have the necessary (but misnamed) methods? */
  1083. result = -ENODEV;
  1084. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
  1085. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
  1086. ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
  1087. result = 0;
  1088. out:
  1089. kfree(path.pointer);
  1090. return result;
  1091. }
  1092. static void acpi_device_set_id(struct acpi_device *device)
  1093. {
  1094. acpi_status status;
  1095. struct acpi_device_info *info;
  1096. struct acpi_pnp_device_id_list *cid_list;
  1097. int i;
  1098. switch (device->device_type) {
  1099. case ACPI_BUS_TYPE_DEVICE:
  1100. if (ACPI_IS_ROOT_DEVICE(device)) {
  1101. acpi_add_id(device, ACPI_SYSTEM_HID);
  1102. break;
  1103. }
  1104. status = acpi_get_object_info(device->handle, &info);
  1105. if (ACPI_FAILURE(status)) {
  1106. printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
  1107. return;
  1108. }
  1109. if (info->valid & ACPI_VALID_HID)
  1110. acpi_add_id(device, info->hardware_id.string);
  1111. if (info->valid & ACPI_VALID_CID) {
  1112. cid_list = &info->compatible_id_list;
  1113. for (i = 0; i < cid_list->count; i++)
  1114. acpi_add_id(device, cid_list->ids[i].string);
  1115. }
  1116. if (info->valid & ACPI_VALID_ADR) {
  1117. device->pnp.bus_address = info->address;
  1118. device->flags.bus_address = 1;
  1119. }
  1120. if (info->valid & ACPI_VALID_UID)
  1121. device->pnp.unique_id = kstrdup(info->unique_id.string,
  1122. GFP_KERNEL);
  1123. kfree(info);
  1124. /*
  1125. * Some devices don't reliably have _HIDs & _CIDs, so add
  1126. * synthetic HIDs to make sure drivers can find them.
  1127. */
  1128. if (acpi_is_video_device(device))
  1129. acpi_add_id(device, ACPI_VIDEO_HID);
  1130. else if (ACPI_SUCCESS(acpi_bay_match(device)))
  1131. acpi_add_id(device, ACPI_BAY_HID);
  1132. else if (ACPI_SUCCESS(acpi_dock_match(device)))
  1133. acpi_add_id(device, ACPI_DOCK_HID);
  1134. else if (!acpi_ibm_smbus_match(device))
  1135. acpi_add_id(device, ACPI_SMBUS_IBM_HID);
  1136. else if (!acpi_device_hid(device) &&
  1137. ACPI_IS_ROOT_DEVICE(device->parent)) {
  1138. acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
  1139. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  1140. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  1141. }
  1142. break;
  1143. case ACPI_BUS_TYPE_POWER:
  1144. acpi_add_id(device, ACPI_POWER_HID);
  1145. break;
  1146. case ACPI_BUS_TYPE_PROCESSOR:
  1147. acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
  1148. break;
  1149. case ACPI_BUS_TYPE_THERMAL:
  1150. acpi_add_id(device, ACPI_THERMAL_HID);
  1151. break;
  1152. case ACPI_BUS_TYPE_POWER_BUTTON:
  1153. acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
  1154. break;
  1155. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  1156. acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
  1157. break;
  1158. }
  1159. }
  1160. static int acpi_device_set_context(struct acpi_device *device)
  1161. {
  1162. acpi_status status;
  1163. /*
  1164. * Context
  1165. * -------
  1166. * Attach this 'struct acpi_device' to the ACPI object. This makes
  1167. * resolutions from handle->device very efficient. Fixed hardware
  1168. * devices have no handles, so we skip them.
  1169. */
  1170. if (!device->handle)
  1171. return 0;
  1172. status = acpi_attach_data(device->handle,
  1173. acpi_bus_data_handler, device);
  1174. if (ACPI_SUCCESS(status))
  1175. return 0;
  1176. printk(KERN_ERR PREFIX "Error attaching device data\n");
  1177. return -ENODEV;
  1178. }
  1179. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  1180. {
  1181. if (!dev)
  1182. return -EINVAL;
  1183. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  1184. device_release_driver(&dev->dev);
  1185. if (!rmdevice)
  1186. return 0;
  1187. /*
  1188. * unbind _ADR-Based Devices when hot removal
  1189. */
  1190. if (dev->flags.bus_address) {
  1191. if ((dev->parent) && (dev->parent->ops.unbind))
  1192. dev->parent->ops.unbind(dev);
  1193. }
  1194. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  1195. return 0;
  1196. }
  1197. /*
  1198. * acpi_hot_add_bind - Bind _ADR-based devices on hot-add.
  1199. * @device: ACPI device node to bind.
  1200. */
  1201. static void acpi_hot_add_bind(struct acpi_device *device)
  1202. {
  1203. if (device->flags.bus_address
  1204. && device->parent && device->parent->ops.bind)
  1205. device->parent->ops.bind(device);
  1206. }
  1207. static int acpi_add_single_object(struct acpi_device **child,
  1208. acpi_handle handle, int type,
  1209. unsigned long long sta,
  1210. enum acpi_bus_add_type add_type)
  1211. {
  1212. int result;
  1213. struct acpi_device *device;
  1214. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1215. device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
  1216. if (!device) {
  1217. printk(KERN_ERR PREFIX "Memory allocation error\n");
  1218. return -ENOMEM;
  1219. }
  1220. INIT_LIST_HEAD(&device->pnp.ids);
  1221. device->device_type = type;
  1222. device->handle = handle;
  1223. device->parent = acpi_bus_get_parent(handle);
  1224. device->add_type = add_type;
  1225. STRUCT_TO_INT(device->status) = sta;
  1226. acpi_device_get_busid(device);
  1227. /*
  1228. * Flags
  1229. * -----
  1230. * Note that we only look for object handles -- cannot evaluate objects
  1231. * until we know the device is present and properly initialized.
  1232. */
  1233. result = acpi_bus_get_flags(device);
  1234. if (result)
  1235. goto end;
  1236. /*
  1237. * Initialize Device
  1238. * -----------------
  1239. * TBD: Synch with Core's enumeration/initialization process.
  1240. */
  1241. acpi_device_set_id(device);
  1242. /*
  1243. * Power Management
  1244. * ----------------
  1245. */
  1246. if (device->flags.power_manageable) {
  1247. result = acpi_bus_get_power_flags(device);
  1248. if (result)
  1249. goto end;
  1250. }
  1251. /*
  1252. * Wakeup device management
  1253. *-----------------------
  1254. */
  1255. acpi_bus_get_wakeup_device_flags(device);
  1256. /*
  1257. * Performance Management
  1258. * ----------------------
  1259. */
  1260. if (device->flags.performance_manageable) {
  1261. result = acpi_bus_get_perf_flags(device);
  1262. if (result)
  1263. goto end;
  1264. }
  1265. if ((result = acpi_device_set_context(device)))
  1266. goto end;
  1267. result = acpi_device_register(device);
  1268. if (device->add_type >= ACPI_BUS_ADD_MATCH)
  1269. acpi_hot_add_bind(device);
  1270. end:
  1271. if (!result) {
  1272. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1273. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1274. "Adding %s [%s] parent %s\n", dev_name(&device->dev),
  1275. (char *) buffer.pointer,
  1276. device->parent ? dev_name(&device->parent->dev) :
  1277. "(null)"));
  1278. kfree(buffer.pointer);
  1279. *child = device;
  1280. } else
  1281. acpi_device_release(&device->dev);
  1282. return result;
  1283. }
  1284. #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
  1285. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
  1286. static void acpi_bus_add_power_resource(acpi_handle handle)
  1287. {
  1288. struct acpi_device *device = NULL;
  1289. acpi_bus_get_device(handle, &device);
  1290. if (!device)
  1291. acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
  1292. ACPI_STA_DEFAULT, ACPI_BUS_ADD_START);
  1293. }
  1294. static int acpi_bus_type_and_status(acpi_handle handle, int *type,
  1295. unsigned long long *sta)
  1296. {
  1297. acpi_status status;
  1298. acpi_object_type acpi_type;
  1299. status = acpi_get_type(handle, &acpi_type);
  1300. if (ACPI_FAILURE(status))
  1301. return -ENODEV;
  1302. switch (acpi_type) {
  1303. case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
  1304. case ACPI_TYPE_DEVICE:
  1305. *type = ACPI_BUS_TYPE_DEVICE;
  1306. status = acpi_bus_get_status_handle(handle, sta);
  1307. if (ACPI_FAILURE(status))
  1308. return -ENODEV;
  1309. break;
  1310. case ACPI_TYPE_PROCESSOR:
  1311. *type = ACPI_BUS_TYPE_PROCESSOR;
  1312. status = acpi_bus_get_status_handle(handle, sta);
  1313. if (ACPI_FAILURE(status))
  1314. return -ENODEV;
  1315. break;
  1316. case ACPI_TYPE_THERMAL:
  1317. *type = ACPI_BUS_TYPE_THERMAL;
  1318. *sta = ACPI_STA_DEFAULT;
  1319. break;
  1320. case ACPI_TYPE_POWER:
  1321. *type = ACPI_BUS_TYPE_POWER;
  1322. *sta = ACPI_STA_DEFAULT;
  1323. break;
  1324. default:
  1325. return -ENODEV;
  1326. }
  1327. return 0;
  1328. }
  1329. static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
  1330. void *context, void **return_value)
  1331. {
  1332. struct acpi_device *device = NULL;
  1333. int type;
  1334. unsigned long long sta;
  1335. acpi_status status;
  1336. int result;
  1337. result = acpi_bus_type_and_status(handle, &type, &sta);
  1338. if (result)
  1339. return AE_OK;
  1340. if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
  1341. !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
  1342. struct acpi_device_wakeup wakeup;
  1343. acpi_handle temp;
  1344. status = acpi_get_handle(handle, "_PRW", &temp);
  1345. if (ACPI_SUCCESS(status))
  1346. acpi_bus_extract_wakeup_device_power_package(handle,
  1347. &wakeup);
  1348. return AE_CTRL_DEPTH;
  1349. }
  1350. acpi_bus_get_device(handle, &device);
  1351. if (!device) {
  1352. acpi_add_single_object(&device, handle, type, sta,
  1353. ACPI_BUS_ADD_BASIC);
  1354. if (!device)
  1355. return AE_CTRL_DEPTH;
  1356. device->add_type = context ?
  1357. ACPI_BUS_ADD_START : ACPI_BUS_ADD_MATCH;
  1358. acpi_hot_add_bind(device);
  1359. }
  1360. if (!*return_value)
  1361. *return_value = device;
  1362. return AE_OK;
  1363. }
  1364. static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
  1365. void *not_used, void **ret_not_used)
  1366. {
  1367. acpi_status status = AE_OK;
  1368. struct acpi_device *device;
  1369. unsigned long long sta_not_used;
  1370. int type_not_used;
  1371. /*
  1372. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1373. * namespace walks prematurely.
  1374. */
  1375. if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
  1376. return AE_OK;
  1377. if (acpi_bus_get_device(handle, &device))
  1378. return AE_CTRL_DEPTH;
  1379. if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
  1380. /* This is a known good platform device. */
  1381. acpi_create_platform_device(device);
  1382. } else if (device_attach(&device->dev)) {
  1383. status = AE_CTRL_DEPTH;
  1384. }
  1385. return status;
  1386. }
  1387. static int acpi_bus_scan(acpi_handle handle, bool start,
  1388. struct acpi_device **child)
  1389. {
  1390. void *device = NULL;
  1391. acpi_status status;
  1392. int ret = -ENODEV;
  1393. status = acpi_bus_check_add(handle, 0, (void *)start, &device);
  1394. if (ACPI_SUCCESS(status))
  1395. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1396. acpi_bus_check_add, NULL, (void *)start,
  1397. &device);
  1398. if (!device)
  1399. goto out;
  1400. ret = 0;
  1401. status = acpi_bus_device_attach(handle, 0, NULL, NULL);
  1402. if (ACPI_SUCCESS(status))
  1403. acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
  1404. acpi_bus_device_attach, NULL, NULL, NULL);
  1405. out:
  1406. if (child)
  1407. *child = device;
  1408. return ret;
  1409. }
  1410. /*
  1411. * acpi_bus_add and acpi_bus_start
  1412. *
  1413. * scan a given ACPI tree and (probably recently hot-plugged)
  1414. * create and add or starts found devices.
  1415. *
  1416. * If no devices were found -ENODEV is returned which does not
  1417. * mean that this is a real error, there just have been no suitable
  1418. * ACPI objects in the table trunk from which the kernel could create
  1419. * a device and add/start an appropriate driver.
  1420. */
  1421. int
  1422. acpi_bus_add(struct acpi_device **child,
  1423. struct acpi_device *parent, acpi_handle handle, int type)
  1424. {
  1425. return acpi_bus_scan(handle, false, child);
  1426. }
  1427. EXPORT_SYMBOL(acpi_bus_add);
  1428. static acpi_status acpi_bus_start_device(acpi_handle handle, u32 lvl,
  1429. void *not_used, void **ret_not_used)
  1430. {
  1431. struct acpi_device *device;
  1432. unsigned long long sta_not_used;
  1433. int type_not_used;
  1434. /*
  1435. * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
  1436. * namespace walks prematurely.
  1437. */
  1438. if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
  1439. return AE_OK;
  1440. if (acpi_bus_get_device(handle, &device))
  1441. return AE_CTRL_DEPTH;
  1442. return acpi_start_single_object(device);
  1443. }
  1444. int acpi_bus_start(struct acpi_device *device)
  1445. {
  1446. if (!device)
  1447. return -EINVAL;
  1448. if (ACPI_SUCCESS(acpi_start_single_object(device)))
  1449. acpi_walk_namespace(ACPI_TYPE_ANY, device->handle,
  1450. ACPI_UINT32_MAX, acpi_bus_start_device,
  1451. NULL, NULL, NULL);
  1452. acpi_update_all_gpes();
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(acpi_bus_start);
  1456. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  1457. {
  1458. acpi_status status;
  1459. struct acpi_device *parent, *child;
  1460. acpi_handle phandle, chandle;
  1461. acpi_object_type type;
  1462. u32 level = 1;
  1463. int err = 0;
  1464. parent = start;
  1465. phandle = start->handle;
  1466. child = chandle = NULL;
  1467. while ((level > 0) && parent && (!err)) {
  1468. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  1469. chandle, &chandle);
  1470. /*
  1471. * If this scope is exhausted then move our way back up.
  1472. */
  1473. if (ACPI_FAILURE(status)) {
  1474. level--;
  1475. chandle = phandle;
  1476. acpi_get_parent(phandle, &phandle);
  1477. child = parent;
  1478. parent = parent->parent;
  1479. if (level == 0)
  1480. err = acpi_bus_remove(child, rmdevice);
  1481. else
  1482. err = acpi_bus_remove(child, 1);
  1483. continue;
  1484. }
  1485. status = acpi_get_type(chandle, &type);
  1486. if (ACPI_FAILURE(status)) {
  1487. continue;
  1488. }
  1489. /*
  1490. * If there is a device corresponding to chandle then
  1491. * parse it (depth-first).
  1492. */
  1493. if (acpi_bus_get_device(chandle, &child) == 0) {
  1494. level++;
  1495. phandle = chandle;
  1496. chandle = NULL;
  1497. parent = child;
  1498. }
  1499. continue;
  1500. }
  1501. return err;
  1502. }
  1503. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1504. static int acpi_bus_scan_fixed(void)
  1505. {
  1506. int result = 0;
  1507. struct acpi_device *device = NULL;
  1508. /*
  1509. * Enumerate all fixed-feature devices.
  1510. */
  1511. if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
  1512. result = acpi_add_single_object(&device, NULL,
  1513. ACPI_BUS_TYPE_POWER_BUTTON,
  1514. ACPI_STA_DEFAULT,
  1515. ACPI_BUS_ADD_START);
  1516. device_init_wakeup(&device->dev, true);
  1517. }
  1518. if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
  1519. result = acpi_add_single_object(&device, NULL,
  1520. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1521. ACPI_STA_DEFAULT,
  1522. ACPI_BUS_ADD_START);
  1523. }
  1524. return result;
  1525. }
  1526. int __init acpi_scan_init(void)
  1527. {
  1528. int result;
  1529. result = bus_register(&acpi_bus_type);
  1530. if (result) {
  1531. /* We don't want to quit even if we failed to add suspend/resume */
  1532. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1533. }
  1534. acpi_power_init();
  1535. acpi_pci_root_init();
  1536. /*
  1537. * Enumerate devices in the ACPI namespace.
  1538. */
  1539. result = acpi_bus_scan(ACPI_ROOT_OBJECT, true, &acpi_root);
  1540. if (!result)
  1541. result = acpi_bus_scan_fixed();
  1542. if (result)
  1543. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1544. else
  1545. acpi_update_all_gpes();
  1546. return result;
  1547. }