scan.c 44 KB

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