proc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <pcmcia/config.h>
  21. //#include <pcmcia/k_compat.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/pnpbios.h>
  28. #include <linux/init.h>
  29. #include <asm/uaccess.h>
  30. #include "pnpbios.h"
  31. static struct proc_dir_entry *proc_pnp = NULL;
  32. static struct proc_dir_entry *proc_pnp_boot = NULL;
  33. static int proc_read_pnpconfig(char *buf, char **start, off_t pos,
  34. int count, int *eof, void *data)
  35. {
  36. struct pnp_isa_config_struc pnps;
  37. if (pnp_bios_isapnp_config(&pnps))
  38. return -EIO;
  39. return snprintf(buf, count,
  40. "structure_revision %d\n"
  41. "number_of_CSNs %d\n"
  42. "ISA_read_data_port 0x%x\n",
  43. pnps.revision,
  44. pnps.no_csns,
  45. pnps.isa_rd_data_port
  46. );
  47. }
  48. static int proc_read_escdinfo(char *buf, char **start, off_t pos,
  49. int count, int *eof, void *data)
  50. {
  51. struct escd_info_struc escd;
  52. if (pnp_bios_escd_info(&escd))
  53. return -EIO;
  54. return snprintf(buf, count,
  55. "min_ESCD_write_size %d\n"
  56. "ESCD_size %d\n"
  57. "NVRAM_base 0x%x\n",
  58. escd.min_escd_write_size,
  59. escd.escd_size,
  60. escd.nv_storage_base
  61. );
  62. }
  63. #define MAX_SANE_ESCD_SIZE (32*1024)
  64. static int proc_read_escd(char *buf, char **start, off_t pos,
  65. int count, int *eof, void *data)
  66. {
  67. struct escd_info_struc escd;
  68. char *tmpbuf;
  69. int escd_size, escd_left_to_read, n;
  70. if (pnp_bios_escd_info(&escd))
  71. return -EIO;
  72. /* sanity check */
  73. if (escd.escd_size > MAX_SANE_ESCD_SIZE) {
  74. printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS escd_info call is too great\n");
  75. return -EFBIG;
  76. }
  77. tmpbuf = kcalloc(1, escd.escd_size, GFP_KERNEL);
  78. if (!tmpbuf) return -ENOMEM;
  79. if (pnp_bios_read_escd(tmpbuf, escd.nv_storage_base)) {
  80. kfree(tmpbuf);
  81. return -EIO;
  82. }
  83. escd_size = (unsigned char)(tmpbuf[0]) + (unsigned char)(tmpbuf[1])*256;
  84. /* sanity check */
  85. if (escd_size > MAX_SANE_ESCD_SIZE) {
  86. printk(KERN_ERR "PnPBIOS: proc_read_escd: ESCD size reported by BIOS read_escd call is too great\n");
  87. return -EFBIG;
  88. }
  89. escd_left_to_read = escd_size - pos;
  90. if (escd_left_to_read < 0) escd_left_to_read = 0;
  91. if (escd_left_to_read == 0) *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 = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
  115. if (!node) 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 "%s Node number 0x%x is out of sequence following node 0x%x. Aborting.\n", "PnPBIOS: proc_read_devices:", (unsigned int)nodenum, (unsigned int)thisnodenum);
  129. *eof = 1;
  130. break;
  131. }
  132. }
  133. kfree(node);
  134. if (nodenum == 0xff)
  135. *eof = 1;
  136. *start = (char *)((off_t)nodenum - pos);
  137. return p - buf;
  138. }
  139. static int proc_read_node(char *buf, char **start, off_t pos,
  140. int count, int *eof, void *data)
  141. {
  142. struct pnp_bios_node *node;
  143. int boot = (long)data >> 8;
  144. u8 nodenum = (long)data;
  145. int len;
  146. node = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
  147. if (!node) return -ENOMEM;
  148. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  149. kfree(node);
  150. return -EIO;
  151. }
  152. len = node->size - sizeof(struct pnp_bios_node);
  153. memcpy(buf, node->data, len);
  154. kfree(node);
  155. return len;
  156. }
  157. static int proc_write_node(struct file *file, const char __user *buf,
  158. unsigned long count, void *data)
  159. {
  160. struct pnp_bios_node *node;
  161. int boot = (long)data >> 8;
  162. u8 nodenum = (long)data;
  163. int ret = count;
  164. node = kcalloc(1, node_info.max_node_size, GFP_KERNEL);
  165. if (!node)
  166. return -ENOMEM;
  167. if (pnp_bios_get_dev_node(&nodenum, boot, node)) {
  168. ret = -EIO;
  169. goto out;
  170. }
  171. if (count != node->size - sizeof(struct pnp_bios_node)) {
  172. ret = -EINVAL;
  173. goto out;
  174. }
  175. if (copy_from_user(node->data, buf, count)) {
  176. ret = -EFAULT;
  177. goto out;
  178. }
  179. if (pnp_bios_set_dev_node(node->handle, boot, node) != 0) {
  180. ret = -EINVAL;
  181. goto out;
  182. }
  183. ret = count;
  184. out:
  185. kfree(node);
  186. return ret;
  187. }
  188. int pnpbios_interface_attach_device(struct pnp_bios_node * node)
  189. {
  190. char name[3];
  191. struct proc_dir_entry *ent;
  192. sprintf(name, "%02x", node->handle);
  193. if (!proc_pnp)
  194. return -EIO;
  195. if ( !pnpbios_dont_use_current_config ) {
  196. ent = create_proc_entry(name, 0, proc_pnp);
  197. if (ent) {
  198. ent->read_proc = proc_read_node;
  199. ent->write_proc = proc_write_node;
  200. ent->data = (void *)(long)(node->handle);
  201. }
  202. }
  203. if (!proc_pnp_boot)
  204. return -EIO;
  205. ent = create_proc_entry(name, 0, proc_pnp_boot);
  206. if (ent) {
  207. ent->read_proc = proc_read_node;
  208. ent->write_proc = proc_write_node;
  209. ent->data = (void *)(long)(node->handle+0x100);
  210. return 0;
  211. }
  212. return -EIO;
  213. }
  214. /*
  215. * When this is called, pnpbios functions are assumed to
  216. * work and the pnpbios_dont_use_current_config flag
  217. * should already have been set to the appropriate value
  218. */
  219. int __init pnpbios_proc_init( void )
  220. {
  221. proc_pnp = proc_mkdir("pnp", proc_bus);
  222. if (!proc_pnp)
  223. return -EIO;
  224. proc_pnp_boot = proc_mkdir("boot", proc_pnp);
  225. if (!proc_pnp_boot)
  226. return -EIO;
  227. create_proc_read_entry("devices", 0, proc_pnp, proc_read_devices, NULL);
  228. create_proc_read_entry("configuration_info", 0, proc_pnp, proc_read_pnpconfig, NULL);
  229. create_proc_read_entry("escd_info", 0, proc_pnp, proc_read_escdinfo, NULL);
  230. create_proc_read_entry("escd", S_IRUSR, proc_pnp, proc_read_escd, NULL);
  231. create_proc_read_entry("legacy_device_resources", 0, proc_pnp, proc_read_legacyres, NULL);
  232. return 0;
  233. }
  234. void __exit pnpbios_proc_exit(void)
  235. {
  236. int i;
  237. char name[3];
  238. if (!proc_pnp)
  239. return;
  240. for (i=0; i<0xff; i++) {
  241. sprintf(name, "%02x", i);
  242. if ( !pnpbios_dont_use_current_config )
  243. remove_proc_entry(name, proc_pnp);
  244. remove_proc_entry(name, proc_pnp_boot);
  245. }
  246. remove_proc_entry("legacy_device_resources", proc_pnp);
  247. remove_proc_entry("escd", proc_pnp);
  248. remove_proc_entry("escd_info", proc_pnp);
  249. remove_proc_entry("configuration_info", proc_pnp);
  250. remove_proc_entry("devices", proc_pnp);
  251. remove_proc_entry("boot", proc_pnp);
  252. remove_proc_entry("pnp", proc_bus);
  253. return;
  254. }