proc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/module.h>
  22. #include <linux/isapnp.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/init.h>
  25. #include <linux/smp_lock.h>
  26. #include <asm/uaccess.h>
  27. extern struct pnp_protocol isapnp_protocol;
  28. static struct proc_dir_entry *isapnp_proc_bus_dir = NULL;
  29. static loff_t isapnp_proc_bus_lseek(struct file *file, loff_t off, int whence)
  30. {
  31. loff_t new = -1;
  32. lock_kernel();
  33. switch (whence) {
  34. case 0:
  35. new = off;
  36. break;
  37. case 1:
  38. new = file->f_pos + off;
  39. break;
  40. case 2:
  41. new = 256 + off;
  42. break;
  43. }
  44. if (new < 0 || new > 256) {
  45. unlock_kernel();
  46. return -EINVAL;
  47. }
  48. unlock_kernel();
  49. return (file->f_pos = new);
  50. }
  51. static ssize_t isapnp_proc_bus_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  52. {
  53. struct inode *ino = file->f_dentry->d_inode;
  54. struct proc_dir_entry *dp = PDE(ino);
  55. struct pnp_dev *dev = dp->data;
  56. int pos = *ppos;
  57. int cnt, size = 256;
  58. if (pos >= size)
  59. return 0;
  60. if (nbytes >= size)
  61. nbytes = size;
  62. if (pos + nbytes > size)
  63. nbytes = size - pos;
  64. cnt = nbytes;
  65. if (!access_ok(VERIFY_WRITE, buf, cnt))
  66. return -EINVAL;
  67. isapnp_cfg_begin(dev->card->number, dev->number);
  68. for ( ; pos < 256 && cnt > 0; pos++, buf++, cnt--) {
  69. unsigned char val;
  70. val = isapnp_read_byte(pos);
  71. __put_user(val, buf);
  72. }
  73. isapnp_cfg_end();
  74. *ppos = pos;
  75. return nbytes;
  76. }
  77. static struct file_operations isapnp_proc_bus_file_operations =
  78. {
  79. .llseek = isapnp_proc_bus_lseek,
  80. .read = isapnp_proc_bus_read,
  81. };
  82. static int isapnp_proc_attach_device(struct pnp_dev *dev)
  83. {
  84. struct pnp_card *bus = dev->card;
  85. struct proc_dir_entry *de, *e;
  86. char name[16];
  87. if (!(de = bus->procdir)) {
  88. sprintf(name, "%02x", bus->number);
  89. de = bus->procdir = proc_mkdir(name, isapnp_proc_bus_dir);
  90. if (!de)
  91. return -ENOMEM;
  92. }
  93. sprintf(name, "%02x", dev->number);
  94. e = dev->procent = create_proc_entry(name, S_IFREG | S_IRUGO, de);
  95. if (!e)
  96. return -ENOMEM;
  97. e->proc_fops = &isapnp_proc_bus_file_operations;
  98. e->owner = THIS_MODULE;
  99. e->data = dev;
  100. e->size = 256;
  101. return 0;
  102. }
  103. #ifdef MODULE
  104. static int __exit isapnp_proc_detach_device(struct pnp_dev *dev)
  105. {
  106. struct pnp_card *bus = dev->card;
  107. struct proc_dir_entry *de;
  108. char name[16];
  109. if (!(de = bus->procdir))
  110. return -EINVAL;
  111. sprintf(name, "%02x", dev->number);
  112. remove_proc_entry(name, de);
  113. return 0;
  114. }
  115. static int __exit isapnp_proc_detach_bus(struct pnp_card *bus)
  116. {
  117. struct proc_dir_entry *de;
  118. char name[16];
  119. if (!(de = bus->procdir))
  120. return -EINVAL;
  121. sprintf(name, "%02x", bus->number);
  122. remove_proc_entry(name, isapnp_proc_bus_dir);
  123. return 0;
  124. }
  125. #endif /* MODULE */
  126. int __init isapnp_proc_init(void)
  127. {
  128. struct pnp_dev *dev;
  129. isapnp_proc_bus_dir = proc_mkdir("isapnp", proc_bus);
  130. protocol_for_each_dev(&isapnp_protocol,dev) {
  131. isapnp_proc_attach_device(dev);
  132. }
  133. return 0;
  134. }
  135. #ifdef MODULE
  136. int __exit isapnp_proc_done(void)
  137. {
  138. struct pnp_dev *dev;
  139. struct pnp_bus *card;
  140. isapnp_for_each_dev(dev) {
  141. isapnp_proc_detach_device(dev);
  142. }
  143. isapnp_for_each_card(card) {
  144. isapnp_proc_detach_bus(card);
  145. }
  146. if (isapnp_proc_bus_dir)
  147. remove_proc_entry("isapnp", proc_bus);
  148. return 0;
  149. }
  150. #endif /* MODULE */