core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pnpbios -- PnP BIOS driver
  4. *
  5. * This driver provides access to Plug-'n'-Play services provided by
  6. * the PnP BIOS firmware, described in the following documents:
  7. * Plug and Play BIOS Specification, Version 1.0A, 5 May 1994
  8. * Plug and Play BIOS Clarification Paper, 6 October 1994
  9. * Compaq Computer Corporation, Phoenix Technologies Ltd., Intel Corp.
  10. *
  11. * Originally (C) 1998 Christian Schmidt <schmidt@digadd.de>
  12. * Modifications (C) 1998 Tom Lees <tom@lpsg.demon.co.uk>
  13. * Minor reorganizations by David Hinds <dahinds@users.sourceforge.net>
  14. * Further modifications (C) 2001, 2002 by:
  15. * Alan Cox <alan@redhat.com>
  16. * Thomas Hood
  17. * Brian Gerst <bgerst@didntduck.org>
  18. *
  19. * Ported to the PnP Layer and several additional improvements (C) 2002
  20. * by Adam Belay <ambx1@neo.rr.com>
  21. */
  22. /* Change Log
  23. *
  24. * Adam Belay - <ambx1@neo.rr.com> - March 16, 2003
  25. * rev 1.01 Only call pnp_bios_dev_node_info once
  26. * Added pnpbios_print_status
  27. * Added several new error messages and info messages
  28. * Added pnpbios_interface_attach_device
  29. * integrated core and proc init system
  30. * Introduced PNPMODE flags
  31. * Removed some useless includes
  32. */
  33. #include <linux/types.h>
  34. #include <linux/init.h>
  35. #include <linux/linkage.h>
  36. #include <linux/kernel.h>
  37. #include <linux/device.h>
  38. #include <linux/pnp.h>
  39. #include <linux/mm.h>
  40. #include <linux/smp.h>
  41. #include <linux/slab.h>
  42. #include <linux/completion.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/dmi.h>
  45. #include <linux/delay.h>
  46. #include <linux/acpi.h>
  47. #include <linux/freezer.h>
  48. #include <linux/kmod.h>
  49. #include <linux/kthread.h>
  50. #include <asm/page.h>
  51. #include <asm/desc.h>
  52. #include <asm/byteorder.h>
  53. #include "../base.h"
  54. #include "pnpbios.h"
  55. /*
  56. *
  57. * PnP BIOS INTERFACE
  58. *
  59. */
  60. static union pnp_bios_install_struct *pnp_bios_install = NULL;
  61. int pnp_bios_present(void)
  62. {
  63. return (pnp_bios_install != NULL);
  64. }
  65. struct pnp_dev_node_info node_info;
  66. /*
  67. *
  68. * DOCKING FUNCTIONS
  69. *
  70. */
  71. static struct completion unload_sem;
  72. /*
  73. * (Much of this belongs in a shared routine somewhere)
  74. */
  75. static int pnp_dock_event(int dock, struct pnp_docking_station_info *info)
  76. {
  77. static char const sbin_pnpbios[] = "/sbin/pnpbios";
  78. char *argv[3], **envp, *buf, *scratch;
  79. int i = 0, value;
  80. if (!(envp = kcalloc(20, sizeof(char *), GFP_KERNEL)))
  81. return -ENOMEM;
  82. if (!(buf = kzalloc(256, GFP_KERNEL))) {
  83. kfree(envp);
  84. return -ENOMEM;
  85. }
  86. /* FIXME: if there are actual users of this, it should be
  87. * integrated into the driver core and use the usual infrastructure
  88. * like sysfs and uevents
  89. */
  90. argv[0] = (char *)sbin_pnpbios;
  91. argv[1] = "dock";
  92. argv[2] = NULL;
  93. /* minimal command environment */
  94. envp[i++] = "HOME=/";
  95. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  96. #ifdef DEBUG
  97. /* hint that policy agent should enter no-stdout debug mode */
  98. envp[i++] = "DEBUG=kernel";
  99. #endif
  100. /* extensible set of named bus-specific parameters,
  101. * supporting multiple driver selection algorithms.
  102. */
  103. scratch = buf;
  104. /* action: add, remove */
  105. envp[i++] = scratch;
  106. scratch += sprintf(scratch, "ACTION=%s", dock ? "add" : "remove") + 1;
  107. /* Report the ident for the dock */
  108. envp[i++] = scratch;
  109. scratch += sprintf(scratch, "DOCK=%x/%x/%x",
  110. info->location_id, info->serial, info->capabilities);
  111. envp[i] = NULL;
  112. value = call_usermodehelper(sbin_pnpbios, argv, envp, UMH_WAIT_EXEC);
  113. kfree(buf);
  114. kfree(envp);
  115. return 0;
  116. }
  117. /*
  118. * Poll the PnP docking at regular intervals
  119. */
  120. static int pnp_dock_thread(void *unused)
  121. {
  122. static struct pnp_docking_station_info now;
  123. int docked = -1, d = 0;
  124. set_freezable();
  125. while (1) {
  126. int status;
  127. /*
  128. * Poll every 2 seconds
  129. */
  130. msleep_interruptible(2000);
  131. if (try_to_freeze())
  132. continue;
  133. status = pnp_bios_dock_station_info(&now);
  134. switch (status) {
  135. /*
  136. * No dock to manage
  137. */
  138. case PNP_FUNCTION_NOT_SUPPORTED:
  139. kthread_complete_and_exit(&unload_sem, 0);
  140. case PNP_SYSTEM_NOT_DOCKED:
  141. d = 0;
  142. break;
  143. case PNP_SUCCESS:
  144. d = 1;
  145. break;
  146. default:
  147. pnpbios_print_status("pnp_dock_thread", status);
  148. printk(KERN_WARNING "PnPBIOS: disabling dock monitoring.\n");
  149. kthread_complete_and_exit(&unload_sem, 0);
  150. }
  151. if (d != docked) {
  152. if (pnp_dock_event(d, &now) == 0) {
  153. docked = d;
  154. #if 0
  155. printk(KERN_INFO
  156. "PnPBIOS: Docking station %stached\n",
  157. docked ? "at" : "de");
  158. #endif
  159. }
  160. }
  161. }
  162. kthread_complete_and_exit(&unload_sem, 0);
  163. }
  164. static int pnpbios_get_resources(struct pnp_dev *dev)
  165. {
  166. u8 nodenum = dev->number;
  167. struct pnp_bios_node *node;
  168. if (!pnpbios_is_dynamic(dev))
  169. return -EPERM;
  170. pnp_dbg(&dev->dev, "get resources\n");
  171. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  172. if (!node)
  173. return -1;
  174. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  175. kfree(node);
  176. return -ENODEV;
  177. }
  178. pnpbios_read_resources_from_node(dev, node);
  179. dev->active = pnp_is_active(dev);
  180. kfree(node);
  181. return 0;
  182. }
  183. static int pnpbios_set_resources(struct pnp_dev *dev)
  184. {
  185. u8 nodenum = dev->number;
  186. struct pnp_bios_node *node;
  187. int ret;
  188. if (!pnpbios_is_dynamic(dev))
  189. return -EPERM;
  190. pnp_dbg(&dev->dev, "set resources\n");
  191. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  192. if (!node)
  193. return -1;
  194. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  195. kfree(node);
  196. return -ENODEV;
  197. }
  198. if (pnpbios_write_resources_to_node(dev, node) < 0) {
  199. kfree(node);
  200. return -1;
  201. }
  202. ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
  203. kfree(node);
  204. if (ret > 0)
  205. ret = -1;
  206. return ret;
  207. }
  208. static void pnpbios_zero_data_stream(struct pnp_bios_node *node)
  209. {
  210. unsigned char *p = (char *)node->data;
  211. unsigned char *end = (char *)(node->data + node->size);
  212. unsigned int len;
  213. int i;
  214. while ((char *)p < (char *)end) {
  215. if (p[0] & 0x80) { /* large tag */
  216. len = (p[2] << 8) | p[1];
  217. p += 3;
  218. } else {
  219. if (((p[0] >> 3) & 0x0f) == 0x0f)
  220. return;
  221. len = p[0] & 0x07;
  222. p += 1;
  223. }
  224. for (i = 0; i < len; i++)
  225. p[i] = 0;
  226. p += len;
  227. }
  228. printk(KERN_ERR
  229. "PnPBIOS: Resource structure did not contain an end tag.\n");
  230. }
  231. static int pnpbios_disable_resources(struct pnp_dev *dev)
  232. {
  233. struct pnp_bios_node *node;
  234. u8 nodenum = dev->number;
  235. int ret;
  236. if (dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
  237. return -EPERM;
  238. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  239. if (!node)
  240. return -ENOMEM;
  241. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  242. kfree(node);
  243. return -ENODEV;
  244. }
  245. pnpbios_zero_data_stream(node);
  246. ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
  247. kfree(node);
  248. if (ret > 0)
  249. ret = -1;
  250. return ret;
  251. }
  252. /* PnP Layer support */
  253. struct pnp_protocol pnpbios_protocol = {
  254. .name = "Plug and Play BIOS",
  255. .get = pnpbios_get_resources,
  256. .set = pnpbios_set_resources,
  257. .disable = pnpbios_disable_resources,
  258. };
  259. static int __init insert_device(struct pnp_bios_node *node)
  260. {
  261. struct pnp_dev *dev;
  262. char id[8];
  263. int error;
  264. /* check if the device is already added */
  265. list_for_each_entry(dev, &pnpbios_protocol.devices, protocol_list) {
  266. if (dev->number == node->handle)
  267. return -EEXIST;
  268. }
  269. pnp_eisa_id_to_string(node->eisa_id & PNP_EISA_ID_MASK, id);
  270. dev = pnp_alloc_dev(&pnpbios_protocol, node->handle, id);
  271. if (!dev)
  272. return -ENOMEM;
  273. pnpbios_parse_data_stream(dev, node);
  274. dev->active = pnp_is_active(dev);
  275. dev->flags = node->flags;
  276. if (!(dev->flags & PNPBIOS_NO_CONFIG))
  277. dev->capabilities |= PNP_CONFIGURABLE;
  278. if (!(dev->flags & PNPBIOS_NO_DISABLE) && pnpbios_is_dynamic(dev))
  279. dev->capabilities |= PNP_DISABLE;
  280. dev->capabilities |= PNP_READ;
  281. if (pnpbios_is_dynamic(dev))
  282. dev->capabilities |= PNP_WRITE;
  283. if (dev->flags & PNPBIOS_REMOVABLE)
  284. dev->capabilities |= PNP_REMOVABLE;
  285. /* clear out the damaged flags */
  286. if (!dev->active)
  287. pnp_init_resources(dev);
  288. error = pnp_add_device(dev);
  289. if (error) {
  290. put_device(&dev->dev);
  291. return error;
  292. }
  293. pnpbios_interface_attach_device(node);
  294. return 0;
  295. }
  296. static void __init build_devlist(void)
  297. {
  298. u8 nodenum;
  299. unsigned int nodes_got = 0;
  300. unsigned int devs = 0;
  301. struct pnp_bios_node *node;
  302. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  303. if (!node)
  304. return;
  305. for (nodenum = 0; nodenum < 0xff;) {
  306. u8 thisnodenum = nodenum;
  307. /* eventually we will want to use PNPMODE_STATIC here but for now
  308. * dynamic will help us catch buggy bioses to add to the blacklist.
  309. */
  310. if (!pnpbios_dont_use_current_config) {
  311. if (pnp_bios_get_dev_node
  312. (&nodenum, (char)PNPMODE_DYNAMIC, node))
  313. break;
  314. } else {
  315. if (pnp_bios_get_dev_node
  316. (&nodenum, (char)PNPMODE_STATIC, node))
  317. break;
  318. }
  319. nodes_got++;
  320. if (insert_device(node) == 0)
  321. devs++;
  322. if (nodenum <= thisnodenum) {
  323. printk(KERN_ERR
  324. "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
  325. (unsigned int)nodenum,
  326. (unsigned int)thisnodenum);
  327. break;
  328. }
  329. }
  330. kfree(node);
  331. printk(KERN_INFO
  332. "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
  333. nodes_got, nodes_got != 1 ? "s" : "", devs);
  334. }
  335. /*
  336. *
  337. * INIT AND EXIT
  338. *
  339. */
  340. static int pnpbios_disabled;
  341. int pnpbios_dont_use_current_config;
  342. static int __init pnpbios_setup(char *str)
  343. {
  344. int invert;
  345. while ((str != NULL) && (*str != '\0')) {
  346. if (strncmp(str, "off", 3) == 0)
  347. pnpbios_disabled = 1;
  348. if (strncmp(str, "on", 2) == 0)
  349. pnpbios_disabled = 0;
  350. invert = (strncmp(str, "no-", 3) == 0);
  351. if (invert)
  352. str += 3;
  353. if (strncmp(str, "curr", 4) == 0)
  354. pnpbios_dont_use_current_config = invert;
  355. str = strchr(str, ',');
  356. if (str != NULL)
  357. str += strspn(str, ", \t");
  358. }
  359. return 1;
  360. }
  361. __setup("pnpbios=", pnpbios_setup);
  362. /* PnP BIOS signature: "$PnP" */
  363. #define PNP_SIGNATURE (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
  364. static int __init pnpbios_probe_system(void)
  365. {
  366. union pnp_bios_install_struct *check;
  367. u8 sum;
  368. int length, i;
  369. printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
  370. /*
  371. * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
  372. * structure and, if one is found, sets up the selectors and
  373. * entry points
  374. */
  375. for (check = (union pnp_bios_install_struct *)__va(0xf0000);
  376. check < (union pnp_bios_install_struct *)__va(0xffff0);
  377. check = (void *)check + 16) {
  378. if (check->fields.signature != PNP_SIGNATURE)
  379. continue;
  380. printk(KERN_INFO
  381. "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
  382. check);
  383. length = check->fields.length;
  384. if (!length) {
  385. printk(KERN_ERR
  386. "PnPBIOS: installation structure is invalid, skipping\n");
  387. continue;
  388. }
  389. for (sum = 0, i = 0; i < length; i++)
  390. sum += check->chars[i];
  391. if (sum) {
  392. printk(KERN_ERR
  393. "PnPBIOS: installation structure is corrupted, skipping\n");
  394. continue;
  395. }
  396. if (check->fields.version < 0x10) {
  397. printk(KERN_WARNING
  398. "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
  399. check->fields.version >> 4,
  400. check->fields.version & 15);
  401. continue;
  402. }
  403. printk(KERN_INFO
  404. "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
  405. check->fields.version >> 4, check->fields.version & 15,
  406. check->fields.pm16cseg, check->fields.pm16offset,
  407. check->fields.pm16dseg);
  408. pnp_bios_install = check;
  409. return 1;
  410. }
  411. printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
  412. return 0;
  413. }
  414. static int __init exploding_pnp_bios(const struct dmi_system_id *d)
  415. {
  416. printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
  417. return 0;
  418. }
  419. static const struct dmi_system_id pnpbios_dmi_table[] __initconst = {
  420. { /* PnPBIOS GPF on boot */
  421. .callback = exploding_pnp_bios,
  422. .ident = "Higraded P14H",
  423. .matches = {
  424. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  425. DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
  426. DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
  427. DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
  428. },
  429. },
  430. { /* PnPBIOS GPF on boot */
  431. .callback = exploding_pnp_bios,
  432. .ident = "ASUS P4P800",
  433. .matches = {
  434. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
  435. DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
  436. },
  437. },
  438. {}
  439. };
  440. static int __init pnpbios_init(void)
  441. {
  442. int ret;
  443. if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table) ||
  444. arch_pnpbios_disabled()) {
  445. printk(KERN_INFO "PnPBIOS: Disabled\n");
  446. return -ENODEV;
  447. }
  448. #ifdef CONFIG_PNPACPI
  449. if (!acpi_disabled && !pnpacpi_disabled) {
  450. pnpbios_disabled = 1;
  451. printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
  452. return -ENODEV;
  453. }
  454. #endif /* CONFIG_ACPI */
  455. /* scan the system for pnpbios support */
  456. if (!pnpbios_probe_system())
  457. return -ENODEV;
  458. /* make preparations for bios calls */
  459. pnpbios_calls_init(pnp_bios_install);
  460. /* read the node info */
  461. ret = pnp_bios_dev_node_info(&node_info);
  462. if (ret) {
  463. printk(KERN_ERR
  464. "PnPBIOS: Unable to get node info. Aborting.\n");
  465. return ret;
  466. }
  467. /* register with the pnp layer */
  468. ret = pnp_register_protocol(&pnpbios_protocol);
  469. if (ret) {
  470. printk(KERN_ERR
  471. "PnPBIOS: Unable to register driver. Aborting.\n");
  472. return ret;
  473. }
  474. /* start the proc interface */
  475. ret = pnpbios_proc_init();
  476. if (ret)
  477. printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
  478. /* scan for pnpbios devices */
  479. build_devlist();
  480. pnp_platform_devices = 1;
  481. return 0;
  482. }
  483. fs_initcall(pnpbios_init);
  484. static int __init pnpbios_thread_init(void)
  485. {
  486. struct task_struct *task;
  487. if (pnpbios_disabled)
  488. return 0;
  489. init_completion(&unload_sem);
  490. task = kthread_run(pnp_dock_thread, NULL, "kpnpbiosd");
  491. return PTR_ERR_OR_ZERO(task);
  492. }
  493. /* Start the kernel thread later: */
  494. device_initcall(pnpbios_thread_init);
  495. EXPORT_SYMBOL(pnpbios_protocol);