core.c 14 KB

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