proc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * /proc/bus/pnp interface for Plug and Play devices
  4. *
  5. * Written by David Hinds, dahinds@users.sourceforge.net
  6. * Modified by Thomas Hood
  7. *
  8. * The .../devices and .../<node> and .../boot/<node> files are
  9. * utilized by the lspnp and setpnp utilities, supplied with the
  10. * pcmcia-cs package.
  11. * http://pcmcia-cs.sourceforge.net
  12. *
  13. * The .../escd file is utilized by the lsescd utility written by
  14. * Gunther Mayer.
  15. *
  16. * The .../legacy_device_resources file is not used yet.
  17. *
  18. * The other files are human-readable.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/pnp.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/init.h>
  28. #include <linux/uaccess.h>
  29. #include "pnpbios.h"
  30. static struct proc_dir_entry *proc_pnp = NULL;
  31. static struct proc_dir_entry *proc_pnp_boot = NULL;
  32. static int pnpconfig_proc_show(struct seq_file *m, void *v)
  33. {
  34. struct pnp_isa_config_struc pnps;
  35. if (pnp_bios_isapnp_config(&pnps))
  36. return -EIO;
  37. seq_printf(m, "structure_revision %d\n"
  38. "number_of_CSNs %d\n"
  39. "ISA_read_data_port 0x%x\n",
  40. pnps.revision, pnps.no_csns, pnps.isa_rd_data_port);
  41. return 0;
  42. }
  43. static int escd_info_proc_show(struct seq_file *m, void *v)
  44. {
  45. struct escd_info_struc escd;
  46. if (pnp_bios_escd_info(&escd))
  47. return -EIO;
  48. seq_printf(m, "min_ESCD_write_size %d\n"
  49. "ESCD_size %d\n"
  50. "NVRAM_base 0x%x\n",
  51. escd.min_escd_write_size,
  52. escd.escd_size, escd.nv_storage_base);
  53. return 0;
  54. }
  55. #define MAX_SANE_ESCD_SIZE (32*1024)
  56. static int escd_proc_show(struct seq_file *m, void *v)
  57. {
  58. struct escd_info_struc escd;
  59. char *tmpbuf;
  60. int escd_size;
  61. if (pnp_bios_escd_info(&escd))
  62. return -EIO;
  63. /* sanity check */
  64. if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
  65. printk(KERN_ERR
  66. "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
  67. return -EFBIG;
  68. }
  69. tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
  70. if (!tmpbuf)
  71. return -ENOMEM;
  72. if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
  73. kfree(tmpbuf);
  74. return -EIO;
  75. }
  76. escd_size =
  77. (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
  78. /* sanity check */
  79. if (escd_size > MAX_SANE_ESCD_SIZE) {
  80. printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
  81. " BIOS read_escd call is too great\n", __func__);
  82. kfree(tmpbuf);
  83. return -EFBIG;
  84. }
  85. seq_write(m, tmpbuf, escd_size);
  86. kfree(tmpbuf);
  87. return 0;
  88. }
  89. static int pnp_legacyres_proc_show(struct seq_file *m, void *v)
  90. {
  91. void *buf;
  92. buf = kmalloc(65536, GFP_KERNEL);
  93. if (!buf)
  94. return -ENOMEM;
  95. if (pnp_bios_get_stat_res(buf)) {
  96. kfree(buf);
  97. return -EIO;
  98. }
  99. seq_write(m, buf, 65536);
  100. kfree(buf);
  101. return 0;
  102. }
  103. static int pnp_devices_proc_show(struct seq_file *m, void *v)
  104. {
  105. struct pnp_bios_node *node;
  106. u8 nodenum;
  107. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  108. if (!node)
  109. return -ENOMEM;
  110. for (nodenum = 0; nodenum < 0xff;) {
  111. u8 thisnodenum = nodenum;
  112. if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
  113. break;
  114. seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
  115. node->handle, node->eisa_id,
  116. node->type_code, node->flags);
  117. if (nodenum <= thisnodenum) {
  118. printk(KERN_ERR
  119. "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
  120. "PnPBIOS: proc_read_devices:",
  121. (unsigned int)nodenum,
  122. (unsigned int)thisnodenum);
  123. break;
  124. }
  125. }
  126. kfree(node);
  127. return 0;
  128. }
  129. static int pnpbios_proc_show(struct seq_file *m, void *v)
  130. {
  131. void *data = m->private;
  132. struct pnp_bios_node *node;
  133. int boot = (long)data >> 8;
  134. u8 nodenum = (long)data;
  135. int len;
  136. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  137. if (!node)
  138. return -ENOMEM;
  139. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  140. kfree(node);
  141. return -EIO;
  142. }
  143. len = node->size - sizeof(struct pnp_bios_node);
  144. seq_write(m, node->data, len);
  145. kfree(node);
  146. return 0;
  147. }
  148. static int pnpbios_proc_open(struct inode *inode, struct file *file)
  149. {
  150. return single_open(file, pnpbios_proc_show, PDE_DATA(inode));
  151. }
  152. static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
  153. size_t count, loff_t *pos)
  154. {
  155. void *data = PDE_DATA(file_inode(file));
  156. struct pnp_bios_node *node;
  157. int boot = (long)data >> 8;
  158. u8 nodenum = (long)data;
  159. int ret = count;
  160. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  161. if (!node)
  162. return -ENOMEM;
  163. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  164. ret = -EIO;
  165. goto out;
  166. }
  167. if (count != node->size - sizeof(struct pnp_bios_node)) {
  168. ret = -EINVAL;
  169. goto out;
  170. }
  171. if (copy_from_user(node->data, buf, count)) {
  172. ret = -EFAULT;
  173. goto out;
  174. }
  175. if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
  176. ret = -EINVAL;
  177. goto out;
  178. }
  179. ret = count;
  180. out:
  181. kfree(node);
  182. return ret;
  183. }
  184. static const struct file_operations pnpbios_proc_fops = {
  185. .owner = THIS_MODULE,
  186. .open = pnpbios_proc_open,
  187. .read = seq_read,
  188. .llseek = seq_lseek,
  189. .release = single_release,
  190. .write = pnpbios_proc_write,
  191. };
  192. int pnpbios_interface_attach_device(struct pnp_bios_node *node)
  193. {
  194. char name[3];
  195. sprintf(name, "%02x", node->handle);
  196. if (!proc_pnp)
  197. return -EIO;
  198. if (!pnpbios_dont_use_current_config) {
  199. proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops,
  200. (void *)(long)(node->handle));
  201. }
  202. if (!proc_pnp_boot)
  203. return -EIO;
  204. if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops,
  205. (void *)(long)(node->handle + 0x100)))
  206. return 0;
  207. return -EIO;
  208. }
  209. /*
  210. * When this is called, pnpbios functions are assumed to
  211. * work and the pnpbios_dont_use_current_config flag
  212. * should already have been set to the appropriate value
  213. */
  214. int __init pnpbios_proc_init(void)
  215. {
  216. proc_pnp = proc_mkdir("bus/pnp", NULL);
  217. if (!proc_pnp)
  218. return -EIO;
  219. proc_pnp_boot = proc_mkdir("boot", proc_pnp);
  220. if (!proc_pnp_boot)
  221. return -EIO;
  222. proc_create_single("devices", 0, proc_pnp, pnp_devices_proc_show);
  223. proc_create_single("configuration_info", 0, proc_pnp,
  224. pnpconfig_proc_show);
  225. proc_create_single("escd_info", 0, proc_pnp, escd_info_proc_show);
  226. proc_create_single("escd", S_IRUSR, proc_pnp, escd_proc_show);
  227. proc_create_single("legacy_device_resources", 0, proc_pnp,
  228. pnp_legacyres_proc_show);
  229. return 0;
  230. }
  231. void __exit pnpbios_proc_exit(void)
  232. {
  233. int i;
  234. char name[3];
  235. if (!proc_pnp)
  236. return;
  237. for (i = 0; i < 0xff; i++) {
  238. sprintf(name, "%02x", i);
  239. if (!pnpbios_dont_use_current_config)
  240. remove_proc_entry(name, proc_pnp);
  241. remove_proc_entry(name, proc_pnp_boot);
  242. }
  243. remove_proc_entry("legacy_device_resources", proc_pnp);
  244. remove_proc_entry("escd", proc_pnp);
  245. remove_proc_entry("escd_info", proc_pnp);
  246. remove_proc_entry("configuration_info", proc_pnp);
  247. remove_proc_entry("devices", proc_pnp);
  248. remove_proc_entry("boot", proc_pnp);
  249. remove_proc_entry("bus/pnp", NULL);
  250. }