scan.c 46 KB

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