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