scan.c 45 KB

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