proc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * ISA Plug & Play support
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <linux/config.h>
  22. #include <linux/module.h>
  23. #include <linux/isapnp.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/init.h>
  26. #include <linux/smp_lock.h>
  27. #include <asm/uaccess.h>
  28. extern struct pnp_protocol isapnp_protocol;
  29. static struct proc_dir_entry *isapnp_proc_bus_dir = NULL;
  30. static loff_t isapnp_proc_bus_lseek(struct file *file, loff_t off, int whence)
  31. {
  32. loff_t new = -1;
  33. lock_kernel();
  34. switch (whence) {
  35. case 0:
  36. new = off;
  37. break;
  38. case 1:
  39. new = file->f_pos + off;
  40. break;
  41. case 2:
  42. new = 256 + off;
  43. break;
  44. }
  45. if (new < 0 || new > 256) {
  46. unlock_kernel();
  47. return -EINVAL;
  48. }
  49. unlock_kernel();
  50. return (file->f_pos = new);
  51. }
  52. static ssize_t isapnp_proc_bus_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  53. {
  54. struct inode *ino = file->f_dentry->d_inode;
  55. struct proc_dir_entry *dp = PDE(ino);
  56. struct pnp_dev *dev = dp->data;
  57. int pos = *ppos;
  58. int cnt, size = 256;
  59. if (pos >= size)
  60. return 0;
  61. if (nbytes >= size)
  62. nbytes = size;
  63. if (pos + nbytes > size)
  64. nbytes = size - pos;
  65. cnt = nbytes;
  66. if (!access_ok(VERIFY_WRITE, buf, cnt))
  67. return -EINVAL;
  68. isapnp_cfg_begin(dev->card->number, dev->number);
  69. for ( ; pos < 256 && cnt > 0; pos++, buf++, cnt--) {
  70. unsigned char val;
  71. val = isapnp_read_byte(pos);
  72. __put_user(val, buf);
  73. }
  74. isapnp_cfg_end();
  75. *ppos = pos;
  76. return nbytes;
  77. }
  78. static struct file_operations isapnp_proc_bus_file_operations =
  79. {
  80. .llseek = isapnp_proc_bus_lseek,
  81. .read = isapnp_proc_bus_read,
  82. };
  83. static int isapnp_proc_attach_device(struct pnp_dev *dev)
  84. {
  85. struct pnp_card *bus = dev->card;
  86. struct proc_dir_entry *de, *e;
  87. char name[16];
  88. if (!(de = bus->procdir)) {
  89. sprintf(name, "%02x", bus->number);
  90. de = bus->procdir = proc_mkdir(name, isapnp_proc_bus_dir);
  91. if (!de)
  92. return -ENOMEM;
  93. }
  94. sprintf(name, "%02x", dev->number);
  95. e = dev->procent = create_proc_entry(name, S_IFREG | S_IRUGO, de);
  96. if (!e)
  97. return -ENOMEM;
  98. e->proc_fops = &isapnp_proc_bus_file_operations;
  99. e->owner = THIS_MODULE;
  100. e->data = dev;
  101. e->size = 256;
  102. return 0;
  103. }
  104. #ifdef MODULE
  105. static int __exit isapnp_proc_detach_device(struct pnp_dev *dev)
  106. {
  107. struct pnp_card *bus = dev->card;
  108. struct proc_dir_entry *de;
  109. char name[16];
  110. if (!(de = bus->procdir))
  111. return -EINVAL;
  112. sprintf(name, "%02x", dev->number);
  113. remove_proc_entry(name, de);
  114. return 0;
  115. }
  116. static int __exit isapnp_proc_detach_bus(struct pnp_card *bus)
  117. {
  118. struct proc_dir_entry *de;
  119. char name[16];
  120. if (!(de = bus->procdir))
  121. return -EINVAL;
  122. sprintf(name, "%02x", bus->number);
  123. remove_proc_entry(name, isapnp_proc_bus_dir);
  124. return 0;
  125. }
  126. #endif /* MODULE */
  127. int __init isapnp_proc_init(void)
  128. {
  129. struct pnp_dev *dev;
  130. isapnp_proc_bus_dir = proc_mkdir("isapnp", proc_bus);
  131. protocol_for_each_dev(&isapnp_protocol,dev) {
  132. isapnp_proc_attach_device(dev);
  133. }
  134. return 0;
  135. }
  136. #ifdef MODULE
  137. int __exit isapnp_proc_done(void)
  138. {
  139. struct pnp_dev *dev;
  140. struct pnp_bus *card;
  141. isapnp_for_each_dev(dev) {
  142. isapnp_proc_detach_device(dev);
  143. }
  144. isapnp_for_each_card(card) {
  145. isapnp_proc_detach_bus(card);
  146. }
  147. if (isapnp_proc_bus_dir)
  148. remove_proc_entry("isapnp", proc_bus);
  149. return 0;
  150. }
  151. #endif /* MODULE */