proc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * http://home.t-online.de/home/gunther.mayer/lsescd
  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/pnpbios.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 proc_read_pnpconfig(char *buf, char **start, off_t pos,
  32. int count, int *eof, void *data)
  33. {
  34. struct pnp_isa_config_struc pnps;
  35. if (pnp_bios_isapnp_config(&pnps))
  36. return -EIO;
  37. return snprintf(buf, count,
  38. "structure_revision %d\n"
  39. "number_of_CSNs %d\n"
  40. "ISA_read_data_port 0x%x\n",
  41. pnps.revision, pnps.no_csns, pnps.isa_rd_data_port);
  42. }
  43. static int proc_read_escdinfo(char *buf, char **start, off_t pos,
  44. int count, int *eof, void *data)
  45. {
  46. struct escd_info_struc escd;
  47. if (pnp_bios_escd_info(&escd))
  48. return -EIO;
  49. return snprintf(buf, count,
  50. "min_ESCD_write_size %d\n"
  51. "ESCD_size %d\n"
  52. "NVRAM_base 0x%x\n",
  53. escd.min_escd_write_size,
  54. escd.escd_size, escd.nv_storage_base);
  55. }
  56. #define MAX_SANE_ESCD_SIZE (32*1024)
  57. static int proc_read_escd(char *buf, char **start, off_t pos,
  58. int count, int *eof, void *data)
  59. {
  60. struct escd_info_struc escd;
  61. char *tmpbuf;
  62. int escd_size, escd_left_to_read, n;
  63. if (pnp_bios_escd_info(&escd))
  64. return -EIO;
  65. /* sanity check */
  66. if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
  67. printk(KERN_ERR
  68. "PnPBIOS: proc_read_escd: ESCD size reported by BIOS escd_info call is too great\n");
  69. return -EFBIG;
  70. }
  71. tmpbuf = kzalloc(escd.escd_size, GFP_KERNEL);
  72. if (!tmpbuf)
  73. return -ENOMEM;
  74. if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
  75. kfree(tmpbuf);
  76. return -EIO;
  77. }
  78. escd_size =
  79. (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1]) * 256;
  80. /* sanity check */
  81. if (escd_size > MAX_SANE_ESCD_SIZE) {
  82. printk(KERN_ERR
  83. "PnPBIOS: proc_read_escd: ESCD size reported by BIOS read_escd call is too great\n");
  84. return -EFBIG;
  85. }
  86. escd_left_to_read = escd_size - pos;
  87. if (escd_left_to_read < 0)
  88. escd_left_to_read = 0;
  89. if (escd_left_to_read == 0)
  90. *eof = 1;
  91. n = min(count, escd_left_to_read);
  92. memcpy(buf, tmpbuf + pos, n);
  93. kfree(tmpbuf);
  94. *start = buf;
  95. return n;
  96. }
  97. static int proc_read_legacyres(char *buf, char **start, off_t pos,
  98. int count, int *eof, void *data)
  99. {
  100. /* Assume that the following won't overflow the buffer */
  101. if (pnp_bios_get_stat_res(buf))
  102. return -EIO;
  103. return count; // FIXME: Return actual length
  104. }
  105. static int proc_read_devices(char *buf, char **start, off_t pos,
  106. int count, int *eof, void *data)
  107. {
  108. struct pnp_bios_node *node;
  109. u8 nodenum;
  110. char *p = buf;
  111. if (pos >= 0xff)
  112. return 0;
  113. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  114. if (!node)
  115. return -ENOMEM;
  116. for (nodenum = pos; nodenum < 0xff;) {
  117. u8 thisnodenum = nodenum;
  118. /* 26 = the number of characters per line sprintf'ed */
  119. if ((p - buf + 26) > count)
  120. break;
  121. if (pnp_bios_get_dev_node(&nodenum, PNPMODE_DYNAMIC, node))
  122. break;
  123. p += sprintf(p, "%02x\t%08x\t%02x:%02x:%02x\t%04x\n",
  124. node->handle, node->eisa_id,
  125. node->type_code[0], node->type_code[1],
  126. node->type_code[2], node->flags);
  127. if (nodenum <= thisnodenum) {
  128. printk(KERN_ERR
  129. "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
  130. "PnPBIOS: proc_read_devices:",
  131. (unsigned int)nodenum,
  132. (unsigned int)thisnodenum);
  133. *eof = 1;
  134. break;
  135. }
  136. }
  137. kfree(node);
  138. if (nodenum == 0xff)
  139. *eof = 1;
  140. *start = (char *)((off_t) nodenum - pos);
  141. return p - buf;
  142. }
  143. static int proc_read_node(char *buf, char **start, off_t pos,
  144. int count, int *eof, void *data)
  145. {
  146. struct pnp_bios_node *node;
  147. int boot = (long)data >> 8;
  148. u8 nodenum = (long)data;
  149. int len;
  150. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  151. if (!node)
  152. return -ENOMEM;
  153. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  154. kfree(node);
  155. return -EIO;
  156. }
  157. len = node->size - sizeof(struct pnp_bios_node);
  158. memcpy(buf, node->data, len);
  159. kfree(node);
  160. return len;
  161. }
  162. static int proc_write_node(struct file *file, const char __user * buf,
  163. unsigned long count, void *data)
  164. {
  165. struct pnp_bios_node *node;
  166. int boot = (long)data >> 8;
  167. u8 nodenum = (long)data;
  168. int ret = count;
  169. node = kzalloc(node_info.max_node_size, GFP_KERNEL);
  170. if (!node)
  171. return -ENOMEM;
  172. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  173. ret = -EIO;
  174. goto out;
  175. }
  176. if (count != node->size - sizeof(struct pnp_bios_node)) {
  177. ret = -EINVAL;
  178. goto out;
  179. }
  180. if (copy_from_user(node->data, buf, count)) {
  181. ret = -EFAULT;
  182. goto out;
  183. }
  184. if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
  185. ret = -EINVAL;
  186. goto out;
  187. }
  188. ret = count;
  189. out:
  190. kfree(node);
  191. return ret;
  192. }
  193. int pnpbios_interface_attach_device(struct pnp_bios_node *node)
  194. {
  195. char name[3];
  196. struct proc_dir_entry *ent;
  197. sprintf(name, "%02x", node->handle);
  198. if (!proc_pnp)
  199. return -EIO;
  200. if (!pnpbios_dont_use_current_config) {
  201. ent = create_proc_entry(name, 0, proc_pnp);
  202. if (ent) {
  203. ent->read_proc = proc_read_node;
  204. ent->write_proc = proc_write_node;
  205. ent->data = (void *)(long)(node->handle);
  206. }
  207. }
  208. if (!proc_pnp_boot)
  209. return -EIO;
  210. ent = create_proc_entry(name, 0, proc_pnp_boot);
  211. if (ent) {
  212. ent->read_proc = proc_read_node;
  213. ent->write_proc = proc_write_node;
  214. ent->data = (void *)(long)(node->handle + 0x100);
  215. return 0;
  216. }
  217. return -EIO;
  218. }
  219. /*
  220. * When this is called, pnpbios functions are assumed to
  221. * work and the pnpbios_dont_use_current_config flag
  222. * should already have been set to the appropriate value
  223. */
  224. int __init pnpbios_proc_init(void)
  225. {
  226. proc_pnp = proc_mkdir("pnp", proc_bus);
  227. if (!proc_pnp)
  228. return -EIO;
  229. proc_pnp_boot = proc_mkdir("boot", proc_pnp);
  230. if (!proc_pnp_boot)
  231. return -EIO;
  232. create_proc_read_entry("devices", 0, proc_pnp, proc_read_devices, NULL);
  233. create_proc_read_entry("configuration_info", 0, proc_pnp,
  234. proc_read_pnpconfig, NULL);
  235. create_proc_read_entry("escd_info", 0, proc_pnp, proc_read_escdinfo,
  236. NULL);
  237. create_proc_read_entry("escd", S_IRUSR, proc_pnp, proc_read_escd, NULL);
  238. create_proc_read_entry("legacy_device_resources", 0, proc_pnp,
  239. proc_read_legacyres, NULL);
  240. return 0;
  241. }
  242. void __exit pnpbios_proc_exit(void)
  243. {
  244. int i;
  245. char name[3];
  246. if (!proc_pnp)
  247. return;
  248. for (i = 0; i < 0xff; i++) {
  249. sprintf(name, "%02x", i);
  250. if (!pnpbios_dont_use_current_config)
  251. remove_proc_entry(name, proc_pnp);
  252. remove_proc_entry(name, proc_pnp_boot);
  253. }
  254. remove_proc_entry("legacy_device_resources", proc_pnp);
  255. remove_proc_entry("escd", proc_pnp);
  256. remove_proc_entry("escd_info", proc_pnp);
  257. remove_proc_entry("configuration_info", proc_pnp);
  258. remove_proc_entry("devices", proc_pnp);
  259. remove_proc_entry("boot", proc_pnp);
  260. remove_proc_entry("pnp", proc_bus);
  261. }