core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. #ifdef CONFIG_HOTPLUG
  85. static struct completion unload_sem;
  86. /*
  87. * (Much of this belongs in a shared routine somewhere)
  88. */
  89. static int pnp_dock_event(int dock, struct pnp_docking_station_info *info)
  90. {
  91. char *argv[3], **envp, *buf, *scratch;
  92. int i = 0, value;
  93. if (!(envp = kcalloc(20, sizeof(char *), GFP_KERNEL)))
  94. return -ENOMEM;
  95. if (!(buf = kzalloc(256, GFP_KERNEL))) {
  96. kfree(envp);
  97. return -ENOMEM;
  98. }
  99. /* FIXME: if there are actual users of this, it should be
  100. * integrated into the driver core and use the usual infrastructure
  101. * like sysfs and uevents
  102. */
  103. argv[0] = "/sbin/pnpbios";
  104. argv[1] = "dock";
  105. argv[2] = NULL;
  106. /* minimal command environment */
  107. envp[i++] = "HOME=/";
  108. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  109. #ifdef DEBUG
  110. /* hint that policy agent should enter no-stdout debug mode */
  111. envp[i++] = "DEBUG=kernel";
  112. #endif
  113. /* extensible set of named bus-specific parameters,
  114. * supporting multiple driver selection algorithms.
  115. */
  116. scratch = buf;
  117. /* action: add, remove */
  118. envp[i++] = scratch;
  119. scratch += sprintf(scratch, "ACTION=%s", dock ? "add" : "remove") + 1;
  120. /* Report the ident for the dock */
  121. envp[i++] = scratch;
  122. scratch += sprintf(scratch, "DOCK=%x/%x/%x",
  123. info->location_id, info->serial, info->capabilities);
  124. envp[i] = NULL;
  125. value = call_usermodehelper(argv [0], argv, envp, UMH_WAIT_EXEC);
  126. kfree(buf);
  127. kfree(envp);
  128. return 0;
  129. }
  130. /*
  131. * Poll the PnP docking at regular intervals
  132. */
  133. static int pnp_dock_thread(void *unused)
  134. {
  135. static struct pnp_docking_station_info now;
  136. int docked = -1, d = 0;
  137. set_freezable();
  138. while (1) {
  139. int status;
  140. /*
  141. * Poll every 2 seconds
  142. */
  143. msleep_interruptible(2000);
  144. if (try_to_freeze())
  145. continue;
  146. status = pnp_bios_dock_station_info(&now);
  147. switch (status) {
  148. /*
  149. * No dock to manage
  150. */
  151. case PNP_FUNCTION_NOT_SUPPORTED:
  152. complete_and_exit(&unload_sem, 0);
  153. case PNP_SYSTEM_NOT_DOCKED:
  154. d = 0;
  155. break;
  156. case PNP_SUCCESS:
  157. d = 1;
  158. break;
  159. default:
  160. pnpbios_print_status("pnp_dock_thread", status);
  161. continue;
  162. }
  163. if (d != docked) {
  164. if (pnp_dock_event(d, &now) == 0) {
  165. docked = d;
  166. #if 0
  167. printk(KERN_INFO
  168. "PnPBIOS: Docking station %stached\n",
  169. docked ? "at" : "de");
  170. #endif
  171. }
  172. }
  173. }
  174. complete_and_exit(&unload_sem, 0);
  175. }
  176. #endif /* CONFIG_HOTPLUG */
  177. static int pnpbios_get_resources(struct pnp_dev *dev)
  178. {
  179. u8 nodenum = dev->number;
  180. struct pnp_bios_node *node;
  181. if (!pnpbios_is_dynamic(dev))
  182. return -EPERM;
  183. pnp_dbg(&dev->dev, "get resources\n");
  184. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  185. if (!node)
  186. return -1;
  187. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  188. kfree(node);
  189. return -ENODEV;
  190. }
  191. pnpbios_read_resources_from_node(dev, node);
  192. dev->active = pnp_is_active(dev);
  193. kfree(node);
  194. return 0;
  195. }
  196. static int pnpbios_set_resources(struct pnp_dev *dev)
  197. {
  198. u8 nodenum = dev->number;
  199. struct pnp_bios_node *node;
  200. int ret;
  201. if (!pnpbios_is_dynamic(dev))
  202. return -EPERM;
  203. pnp_dbg(&dev->dev, "set resources\n");
  204. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  205. if (!node)
  206. return -1;
  207. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  208. kfree(node);
  209. return -ENODEV;
  210. }
  211. if (pnpbios_write_resources_to_node(dev, node) < 0) {
  212. kfree(node);
  213. return -1;
  214. }
  215. ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
  216. kfree(node);
  217. if (ret > 0)
  218. ret = -1;
  219. return ret;
  220. }
  221. static void pnpbios_zero_data_stream(struct pnp_bios_node *node)
  222. {
  223. unsigned char *p = (char *)node->data;
  224. unsigned char *end = (char *)(node->data + node->size);
  225. unsigned int len;
  226. int i;
  227. while ((char *)p < (char *)end) {
  228. if (p[0] & 0x80) { /* large tag */
  229. len = (p[2] << 8) | p[1];
  230. p += 3;
  231. } else {
  232. if (((p[0] >> 3) & 0x0f) == 0x0f)
  233. return;
  234. len = p[0] & 0x07;
  235. p += 1;
  236. }
  237. for (i = 0; i < len; i++)
  238. p[i] = 0;
  239. p += len;
  240. }
  241. printk(KERN_ERR
  242. "PnPBIOS: Resource structure did not contain an end tag.\n");
  243. }
  244. static int pnpbios_disable_resources(struct pnp_dev *dev)
  245. {
  246. struct pnp_bios_node *node;
  247. u8 nodenum = dev->number;
  248. int ret;
  249. if (dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
  250. return -EPERM;
  251. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  252. if (!node)
  253. return -ENOMEM;
  254. if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
  255. kfree(node);
  256. return -ENODEV;
  257. }
  258. pnpbios_zero_data_stream(node);
  259. ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
  260. kfree(node);
  261. if (ret > 0)
  262. ret = -1;
  263. return ret;
  264. }
  265. /* PnP Layer support */
  266. struct pnp_protocol pnpbios_protocol = {
  267. .name = "Plug and Play BIOS",
  268. .get = pnpbios_get_resources,
  269. .set = pnpbios_set_resources,
  270. .disable = pnpbios_disable_resources,
  271. };
  272. static int __init insert_device(struct pnp_bios_node *node)
  273. {
  274. struct list_head *pos;
  275. struct pnp_dev *dev;
  276. char id[8];
  277. /* check if the device is already added */
  278. list_for_each(pos, &pnpbios_protocol.devices) {
  279. dev = list_entry(pos, struct pnp_dev, protocol_list);
  280. if (dev->number == node->handle)
  281. return -1;
  282. }
  283. pnp_eisa_id_to_string(node->eisa_id & PNP_EISA_ID_MASK, id);
  284. dev = pnp_alloc_dev(&pnpbios_protocol, node->handle, id);
  285. if (!dev)
  286. return -1;
  287. pnpbios_parse_data_stream(dev, node);
  288. dev->active = pnp_is_active(dev);
  289. dev->flags = node->flags;
  290. if (!(dev->flags & PNPBIOS_NO_CONFIG))
  291. dev->capabilities |= PNP_CONFIGURABLE;
  292. if (!(dev->flags & PNPBIOS_NO_DISABLE) && pnpbios_is_dynamic(dev))
  293. dev->capabilities |= PNP_DISABLE;
  294. dev->capabilities |= PNP_READ;
  295. if (pnpbios_is_dynamic(dev))
  296. dev->capabilities |= PNP_WRITE;
  297. if (dev->flags & PNPBIOS_REMOVABLE)
  298. dev->capabilities |= PNP_REMOVABLE;
  299. /* clear out the damaged flags */
  300. if (!dev->active)
  301. pnp_init_resources(dev);
  302. pnp_add_device(dev);
  303. pnpbios_interface_attach_device(node);
  304. return 0;
  305. }
  306. static void __init build_devlist(void)
  307. {
  308. u8 nodenum;
  309. unsigned int nodes_got = 0;
  310. unsigned int devs = 0;
  311. struct pnp_bios_node *node;
  312. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  313. if (!node)
  314. return;
  315. for (nodenum = 0; nodenum < 0xff;) {
  316. u8 thisnodenum = nodenum;
  317. /* eventually we will want to use PNPMODE_STATIC here but for now
  318. * dynamic will help us catch buggy bioses to add to the blacklist.
  319. */
  320. if (!pnpbios_dont_use_current_config) {
  321. if (pnp_bios_get_dev_node
  322. (&nodenum, (char)PNPMODE_DYNAMIC, node))
  323. break;
  324. } else {
  325. if (pnp_bios_get_dev_node
  326. (&nodenum, (char)PNPMODE_STATIC, node))
  327. break;
  328. }
  329. nodes_got++;
  330. if (insert_device(node) == 0)
  331. devs++;
  332. if (nodenum <= thisnodenum) {
  333. printk(KERN_ERR
  334. "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
  335. (unsigned int)nodenum,
  336. (unsigned int)thisnodenum);
  337. break;
  338. }
  339. }
  340. kfree(node);
  341. printk(KERN_INFO
  342. "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
  343. nodes_got, nodes_got != 1 ? "s" : "", devs);
  344. }
  345. /*
  346. *
  347. * INIT AND EXIT
  348. *
  349. */
  350. static int pnpbios_disabled;
  351. int pnpbios_dont_use_current_config;
  352. static int __init pnpbios_setup(char *str)
  353. {
  354. int invert;
  355. while ((str != NULL) && (*str != '\0')) {
  356. if (strncmp(str, "off", 3) == 0)
  357. pnpbios_disabled = 1;
  358. if (strncmp(str, "on", 2) == 0)
  359. pnpbios_disabled = 0;
  360. invert = (strncmp(str, "no-", 3) == 0);
  361. if (invert)
  362. str += 3;
  363. if (strncmp(str, "curr", 4) == 0)
  364. pnpbios_dont_use_current_config = invert;
  365. str = strchr(str, ',');
  366. if (str != NULL)
  367. str += strspn(str, ", \t");
  368. }
  369. return 1;
  370. }
  371. __setup("pnpbios=", pnpbios_setup);
  372. /* PnP BIOS signature: "$PnP" */
  373. #define PNP_SIGNATURE (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
  374. static int __init pnpbios_probe_system(void)
  375. {
  376. union pnp_bios_install_struct *check;
  377. u8 sum;
  378. int length, i;
  379. printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
  380. /*
  381. * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
  382. * structure and, if one is found, sets up the selectors and
  383. * entry points
  384. */
  385. for (check = (union pnp_bios_install_struct *)__va(0xf0000);
  386. check < (union pnp_bios_install_struct *)__va(0xffff0);
  387. check = (void *)check + 16) {
  388. if (check->fields.signature != PNP_SIGNATURE)
  389. continue;
  390. printk(KERN_INFO
  391. "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
  392. check);
  393. length = check->fields.length;
  394. if (!length) {
  395. printk(KERN_ERR
  396. "PnPBIOS: installation structure is invalid, skipping\n");
  397. continue;
  398. }
  399. for (sum = 0, i = 0; i < length; i++)
  400. sum += check->chars[i];
  401. if (sum) {
  402. printk(KERN_ERR
  403. "PnPBIOS: installation structure is corrupted, skipping\n");
  404. continue;
  405. }
  406. if (check->fields.version < 0x10) {
  407. printk(KERN_WARNING
  408. "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
  409. check->fields.version >> 4,
  410. check->fields.version & 15);
  411. continue;
  412. }
  413. printk(KERN_INFO
  414. "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
  415. check->fields.version >> 4, check->fields.version & 15,
  416. check->fields.pm16cseg, check->fields.pm16offset,
  417. check->fields.pm16dseg);
  418. pnp_bios_install = check;
  419. return 1;
  420. }
  421. printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
  422. return 0;
  423. }
  424. static int __init exploding_pnp_bios(const struct dmi_system_id *d)
  425. {
  426. printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
  427. return 0;
  428. }
  429. static struct dmi_system_id pnpbios_dmi_table[] __initdata = {
  430. { /* PnPBIOS GPF on boot */
  431. .callback = exploding_pnp_bios,
  432. .ident = "Higraded P14H",
  433. .matches = {
  434. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  435. DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
  436. DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
  437. DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
  438. },
  439. },
  440. { /* PnPBIOS GPF on boot */
  441. .callback = exploding_pnp_bios,
  442. .ident = "ASUS P4P800",
  443. .matches = {
  444. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
  445. DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
  446. },
  447. },
  448. {}
  449. };
  450. static int __init pnpbios_init(void)
  451. {
  452. int ret;
  453. #if defined(CONFIG_PPC)
  454. if (check_legacy_ioport(PNPBIOS_BASE))
  455. return -ENODEV;
  456. #endif
  457. if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table) ||
  458. paravirt_enabled()) {
  459. printk(KERN_INFO "PnPBIOS: Disabled\n");
  460. return -ENODEV;
  461. }
  462. #ifdef CONFIG_PNPACPI
  463. if (!acpi_disabled && !pnpacpi_disabled) {
  464. pnpbios_disabled = 1;
  465. printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
  466. return -ENODEV;
  467. }
  468. #endif /* CONFIG_ACPI */
  469. /* scan the system for pnpbios support */
  470. if (!pnpbios_probe_system())
  471. return -ENODEV;
  472. /* make preparations for bios calls */
  473. pnpbios_calls_init(pnp_bios_install);
  474. /* read the node info */
  475. ret = pnp_bios_dev_node_info(&node_info);
  476. if (ret) {
  477. printk(KERN_ERR
  478. "PnPBIOS: Unable to get node info. Aborting.\n");
  479. return ret;
  480. }
  481. /* register with the pnp layer */
  482. ret = pnp_register_protocol(&pnpbios_protocol);
  483. if (ret) {
  484. printk(KERN_ERR
  485. "PnPBIOS: Unable to register driver. Aborting.\n");
  486. return ret;
  487. }
  488. /* start the proc interface */
  489. ret = pnpbios_proc_init();
  490. if (ret)
  491. printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
  492. /* scan for pnpbios devices */
  493. build_devlist();
  494. pnp_platform_devices = 1;
  495. return 0;
  496. }
  497. fs_initcall(pnpbios_init);
  498. static int __init pnpbios_thread_init(void)
  499. {
  500. #if defined(CONFIG_PPC)
  501. if (check_legacy_ioport(PNPBIOS_BASE))
  502. return 0;
  503. #endif
  504. if (pnpbios_disabled)
  505. return 0;
  506. #ifdef CONFIG_HOTPLUG
  507. {
  508. struct task_struct *task;
  509. init_completion(&unload_sem);
  510. task = kthread_run(pnp_dock_thread, NULL, "kpnpbiosd");
  511. if (IS_ERR(task))
  512. return PTR_ERR(task);
  513. }
  514. #endif
  515. return 0;
  516. }
  517. /* Start the kernel thread later: */
  518. module_init(pnpbios_thread_init);
  519. EXPORT_SYMBOL(pnpbios_protocol);