core.c 14 KB

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