core.c 15 KB

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