proc.c 8.2 KB

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