proc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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%02x:%02x:%02x\t%04x\n",
  158. node->handle, node->eisa_id,
  159. node->type_code[0], node->type_code[1],
  160. node->type_code[2], 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(inode)->data);
  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(file->f_path.dentry->d_inode)->data;
  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. }