core.c 14 KB

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