proc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* drivers/nubus/proc.c: Proc FS interface for NuBus.
  2. By David Huggins-Daines <dhd@debian.org>
  3. Much code and many ideas from drivers/pci/proc.c:
  4. Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  5. This is initially based on the Zorro and PCI interfaces. However,
  6. it works somewhat differently. The intent is to provide a
  7. structure in /proc analogous to the structure of the NuBus ROM
  8. resources.
  9. Therefore each NuBus device is in fact a directory, which may in
  10. turn contain subdirectories. The "files" correspond to NuBus
  11. resource records. For those types of records which we know how to
  12. convert to formats that are meaningful to userspace (mostly just
  13. icons) these files will provide "cooked" data. Otherwise they will
  14. simply provide raw access (read-only of course) to the ROM. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/nubus.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/byteorder.h>
  24. static int
  25. nubus_devices_proc_show(struct seq_file *m, void *v)
  26. {
  27. struct nubus_dev *dev = nubus_devices;
  28. while (dev) {
  29. seq_printf(m, "%x\t%04x %04x %04x %04x",
  30. dev->board->slot,
  31. dev->category,
  32. dev->type,
  33. dev->dr_sw,
  34. dev->dr_hw);
  35. seq_printf(m, "\t%08lx\n", dev->board->slot_addr);
  36. dev = dev->next;
  37. }
  38. return 0;
  39. }
  40. static int nubus_devices_proc_open(struct inode *inode, struct file *file)
  41. {
  42. return single_open(file, nubus_devices_proc_show, NULL);
  43. }
  44. static const struct file_operations nubus_devices_proc_fops = {
  45. .open = nubus_devices_proc_open,
  46. .read = seq_read,
  47. .llseek = seq_lseek,
  48. .release = single_release,
  49. };
  50. static struct proc_dir_entry *proc_bus_nubus_dir;
  51. static const struct file_operations nubus_proc_subdir_fops = {
  52. #warning Need to set some I/O handlers here
  53. };
  54. static void nubus_proc_subdir(struct nubus_dev* dev,
  55. struct proc_dir_entry* parent,
  56. struct nubus_dir* dir)
  57. {
  58. struct nubus_dirent ent;
  59. /* Some of these are directories, others aren't */
  60. while (nubus_readdir(dir, &ent) != -1) {
  61. char name[8];
  62. struct proc_dir_entry* e;
  63. sprintf(name, "%x", ent.type);
  64. e = proc_create(name, S_IFREG | S_IRUGO | S_IWUSR, parent,
  65. &nubus_proc_subdir_fops);
  66. if (!e)
  67. return;
  68. }
  69. }
  70. /* Can't do this recursively since the root directory is structured
  71. somewhat differently from the subdirectories */
  72. static void nubus_proc_populate(struct nubus_dev* dev,
  73. struct proc_dir_entry* parent,
  74. struct nubus_dir* root)
  75. {
  76. struct nubus_dirent ent;
  77. /* We know these are all directories (board resource + one or
  78. more functional resources) */
  79. while (nubus_readdir(root, &ent) != -1) {
  80. char name[8];
  81. struct proc_dir_entry* e;
  82. struct nubus_dir dir;
  83. sprintf(name, "%x", ent.type);
  84. e = proc_mkdir(name, parent);
  85. if (!e) return;
  86. /* And descend */
  87. if (nubus_get_subdir(&ent, &dir) == -1) {
  88. /* This shouldn't happen */
  89. printk(KERN_ERR "NuBus root directory node %x:%x has no subdir!\n",
  90. dev->board->slot, ent.type);
  91. continue;
  92. } else {
  93. nubus_proc_subdir(dev, e, &dir);
  94. }
  95. }
  96. }
  97. int nubus_proc_attach_device(struct nubus_dev *dev)
  98. {
  99. struct proc_dir_entry *e;
  100. struct nubus_dir root;
  101. char name[8];
  102. if (dev == NULL) {
  103. printk(KERN_ERR
  104. "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
  105. return -1;
  106. }
  107. if (dev->board == NULL) {
  108. printk(KERN_ERR
  109. "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
  110. printk("dev = %p, dev->board = %p\n", dev, dev->board);
  111. return -1;
  112. }
  113. /* Create a directory */
  114. sprintf(name, "%x", dev->board->slot);
  115. e = dev->procdir = proc_mkdir(name, proc_bus_nubus_dir);
  116. if (!e)
  117. return -ENOMEM;
  118. /* Now recursively populate it with files */
  119. nubus_get_root_dir(dev->board, &root);
  120. nubus_proc_populate(dev, e, &root);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(nubus_proc_attach_device);
  124. /*
  125. * /proc/nubus stuff
  126. */
  127. static int nubus_proc_show(struct seq_file *m, void *v)
  128. {
  129. const struct nubus_board *board = v;
  130. /* Display header on line 1 */
  131. if (v == SEQ_START_TOKEN)
  132. seq_puts(m, "Nubus devices found:\n");
  133. else
  134. seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
  135. return 0;
  136. }
  137. static void *nubus_proc_start(struct seq_file *m, loff_t *_pos)
  138. {
  139. struct nubus_board *board;
  140. unsigned pos;
  141. if (*_pos > LONG_MAX)
  142. return NULL;
  143. pos = *_pos;
  144. if (pos == 0)
  145. return SEQ_START_TOKEN;
  146. for (board = nubus_boards; board; board = board->next)
  147. if (--pos == 0)
  148. break;
  149. return board;
  150. }
  151. static void *nubus_proc_next(struct seq_file *p, void *v, loff_t *_pos)
  152. {
  153. /* Walk the list of NuBus boards */
  154. struct nubus_board *board = v;
  155. ++*_pos;
  156. if (v == SEQ_START_TOKEN)
  157. board = nubus_boards;
  158. else if (board)
  159. board = board->next;
  160. return board;
  161. }
  162. static void nubus_proc_stop(struct seq_file *p, void *v)
  163. {
  164. }
  165. static const struct seq_operations nubus_proc_seqops = {
  166. .start = nubus_proc_start,
  167. .next = nubus_proc_next,
  168. .stop = nubus_proc_stop,
  169. .show = nubus_proc_show,
  170. };
  171. static int nubus_proc_open(struct inode *inode, struct file *file)
  172. {
  173. return seq_open(file, &nubus_proc_seqops);
  174. }
  175. static const struct file_operations nubus_proc_fops = {
  176. .open = nubus_proc_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = seq_release,
  180. };
  181. void __init proc_bus_nubus_add_devices(void)
  182. {
  183. struct nubus_dev *dev;
  184. for(dev = nubus_devices; dev; dev = dev->next)
  185. nubus_proc_attach_device(dev);
  186. }
  187. void __init nubus_proc_init(void)
  188. {
  189. proc_create("nubus", 0, NULL, &nubus_proc_fops);
  190. if (!MACH_IS_MAC)
  191. return;
  192. proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
  193. proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
  194. proc_bus_nubus_add_devices();
  195. }