proc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 pnpconfig_proc_open(struct inode *inode, struct file *file)
  44. {
  45. return single_open(file, pnpconfig_proc_show, NULL);
  46. }
  47. static const struct file_operations pnpconfig_proc_fops = {
  48. .owner = THIS_MODULE,
  49. .open = pnpconfig_proc_open,
  50. .read = seq_read,
  51. .llseek = seq_lseek,
  52. .release = single_release,
  53. };
  54. static int escd_info_proc_show(struct seq_file *m, void *v)
  55. {
  56. struct escd_info_struc escd;
  57. if (pnp_bios_escd_info(&escd))
  58. return -EIO;
  59. seq_printf(m, "min_ESCD_write_size %d\n"
  60. "ESCD_size %d\n"
  61. "NVRAM_base 0x%x\n",
  62. escd.min_escd_write_size,
  63. escd.escd_size, escd.nv_storage_base);
  64. return 0;
  65. }
  66. static int escd_info_proc_open(struct inode *inode, struct file *file)
  67. {
  68. return single_open(file, escd_info_proc_show, NULL);
  69. }
  70. static const struct file_operations escd_info_proc_fops = {
  71. .owner = THIS_MODULE,
  72. .open = escd_info_proc_open,
  73. .read = seq_read,
  74. .llseek = seq_lseek,
  75. .release = single_release,
  76. };
  77. #define MAX_SANE_ESCD_SIZE (32*1024)
  78. static int escd_proc_show(struct seq_file *m, void *v)
  79. {
  80. struct escd_info_struc escd;
  81. char *tmpbuf;
  82. int escd_size;
  83. if (pnp_bios_escd_info(&escd))
  84. return -EIO;
  85. /* sanity check */
  86. if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
  87. printk(KERN_ERR
  88. "PnPBIOS: %s: ESCD size reported by BIOS escd_info call is too great\n", __func__);
  89. return -EFBIG;
  90. }
  91. tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
  92. if (!tmpbuf)
  93. return -ENOMEM;
  94. if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
  95. kfree(tmpbuf);
  96. return -EIO;
  97. }
  98. escd_size =
  99. (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
  100. /* sanity check */
  101. if (escd_size > MAX_SANE_ESCD_SIZE) {
  102. printk(KERN_ERR "PnPBIOS: %s: ESCD size reported by"
  103. " BIOS read_escd call is too great\n", __func__);
  104. kfree(tmpbuf);
  105. return -EFBIG;
  106. }
  107. seq_write(m, tmpbuf, escd_size);
  108. kfree(tmpbuf);
  109. return 0;
  110. }
  111. static int escd_proc_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, escd_proc_show, NULL);
  114. }
  115. static const struct file_operations escd_proc_fops = {
  116. .owner = THIS_MODULE,
  117. .open = escd_proc_open,
  118. .read = seq_read,
  119. .llseek = seq_lseek,
  120. .release = single_release,
  121. };
  122. static int pnp_legacyres_proc_show(struct seq_file *m, void *v)
  123. {
  124. void *buf;
  125. buf = kmalloc(65536, GFP_KERNEL);
  126. if (!buf)
  127. return -ENOMEM;
  128. if (pnp_bios_get_stat_res(buf)) {
  129. kfree(buf);
  130. return -EIO;
  131. }
  132. seq_write(m, buf, 65536);
  133. kfree(buf);
  134. return 0;
  135. }
  136. static int pnp_legacyres_proc_open(struct inode *inode, struct file *file)
  137. {
  138. return single_open(file, pnp_legacyres_proc_show, NULL);
  139. }
  140. static const struct file_operations pnp_legacyres_proc_fops = {
  141. .owner = THIS_MODULE,
  142. .open = pnp_legacyres_proc_open,
  143. .read = seq_read,
  144. .llseek = seq_lseek,
  145. .release = single_release,
  146. };
  147. static int pnp_devices_proc_show(struct seq_file *m, void *v)
  148. {
  149. struct pnp_bios_node *node;
  150. u8 nodenum;
  151. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  152. if (!node)
  153. return -ENOMEM;
  154. for (nodenum = 0; nodenum < 0xff;) {
  155. u8 thisnodenum = nodenum;
  156. if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
  157. break;
  158. seq_printf(m, "%02x\t%08x\t%3phC\t%04x\n",
  159. node->handle, node->eisa_id,
  160. node->type_code, node->flags);
  161. if (nodenum <= thisnodenum) {
  162. printk(KERN_ERR
  163. "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
  164. "PnPBIOS: proc_read_devices:",
  165. (unsigned int)nodenum,
  166. (unsigned int)thisnodenum);
  167. break;
  168. }
  169. }
  170. kfree(node);
  171. return 0;
  172. }
  173. static int pnp_devices_proc_open(struct inode *inode, struct file *file)
  174. {
  175. return single_open(file, pnp_devices_proc_show, NULL);
  176. }
  177. static const struct file_operations pnp_devices_proc_fops = {
  178. .owner = THIS_MODULE,
  179. .open = pnp_devices_proc_open,
  180. .read = seq_read,
  181. .llseek = seq_lseek,
  182. .release = single_release,
  183. };
  184. static int pnpbios_proc_show(struct seq_file *m, void *v)
  185. {
  186. void *data = m->private;
  187. struct pnp_bios_node *node;
  188. int boot = (long)data >> 8;
  189. u8 nodenum = (long)data;
  190. int len;
  191. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  192. if (!node)
  193. return -ENOMEM;
  194. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  195. kfree(node);
  196. return -EIO;
  197. }
  198. len = node->size - sizeof(struct pnp_bios_node);
  199. seq_write(m, node->data, len);
  200. kfree(node);
  201. return 0;
  202. }
  203. static int pnpbios_proc_open(struct inode *inode, struct file *file)
  204. {
  205. return single_open(file, pnpbios_proc_show, PDE_DATA(inode));
  206. }
  207. static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
  208. size_t count, loff_t *pos)
  209. {
  210. void *data = PDE_DATA(file_inode(file));
  211. struct pnp_bios_node *node;
  212. int boot = (long)data >> 8;
  213. u8 nodenum = (long)data;
  214. int ret = count;
  215. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  216. if (!node)
  217. return -ENOMEM;
  218. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  219. ret = -EIO;
  220. goto out;
  221. }
  222. if (count != node->size - sizeof(struct pnp_bios_node)) {
  223. ret = -EINVAL;
  224. goto out;
  225. }
  226. if (copy_from_user(node->data, buf, count)) {
  227. ret = -EFAULT;
  228. goto out;
  229. }
  230. if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
  231. ret = -EINVAL;
  232. goto out;
  233. }
  234. ret = count;
  235. out:
  236. kfree(node);
  237. return ret;
  238. }
  239. static const struct file_operations pnpbios_proc_fops = {
  240. .owner = THIS_MODULE,
  241. .open = pnpbios_proc_open,
  242. .read = seq_read,
  243. .llseek = seq_lseek,
  244. .release = single_release,
  245. .write = pnpbios_proc_write,
  246. };
  247. int pnpbios_interface_attach_device(struct pnp_bios_node *node)
  248. {
  249. char name[3];
  250. sprintf(name, "%02x", node->handle);
  251. if (!proc_pnp)
  252. return -EIO;
  253. if (!pnpbios_dont_use_current_config) {
  254. proc_create_data(name, 0644, proc_pnp, &pnpbios_proc_fops,
  255. (void *)(long)(node->handle));
  256. }
  257. if (!proc_pnp_boot)
  258. return -EIO;
  259. if (proc_create_data(name, 0644, proc_pnp_boot, &pnpbios_proc_fops,
  260. (void *)(long)(node->handle + 0x100)))
  261. return 0;
  262. return -EIO;
  263. }
  264. /*
  265. * When this is called, pnpbios functions are assumed to
  266. * work and the pnpbios_dont_use_current_config flag
  267. * should already have been set to the appropriate value
  268. */
  269. int __init pnpbios_proc_init(void)
  270. {
  271. proc_pnp = proc_mkdir("bus/pnp", NULL);
  272. if (!proc_pnp)
  273. return -EIO;
  274. proc_pnp_boot = proc_mkdir("boot", proc_pnp);
  275. if (!proc_pnp_boot)
  276. return -EIO;
  277. proc_create("devices", 0, proc_pnp, &pnp_devices_proc_fops);
  278. proc_create("configuration_info", 0, proc_pnp, &pnpconfig_proc_fops);
  279. proc_create("escd_info", 0, proc_pnp, &escd_info_proc_fops);
  280. proc_create("escd", S_IRUSR, proc_pnp, &escd_proc_fops);
  281. proc_create("legacy_device_resources", 0, proc_pnp, &pnp_legacyres_proc_fops);
  282. return 0;
  283. }
  284. void __exit pnpbios_proc_exit(void)
  285. {
  286. int i;
  287. char name[3];
  288. if (!proc_pnp)
  289. return;
  290. for (i = 0; i < 0xff; i++) {
  291. sprintf(name, "%02x", i);
  292. if (!pnpbios_dont_use_current_config)
  293. remove_proc_entry(name, proc_pnp);
  294. remove_proc_entry(name, proc_pnp_boot);
  295. }
  296. remove_proc_entry("legacy_device_resources", proc_pnp);
  297. remove_proc_entry("escd", proc_pnp);
  298. remove_proc_entry("escd_info", proc_pnp);
  299. remove_proc_entry("configuration_info", proc_pnp);
  300. remove_proc_entry("devices", proc_pnp);
  301. remove_proc_entry("boot", proc_pnp);
  302. remove_proc_entry("bus/pnp", NULL);
  303. }