scan.c 46 KB

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