proc.c 7.2 KB

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