core.c 14 KB

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