proc.c 5.7 KB

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