proc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 board function gets a directory, which may in turn
  11. contain subdirectories. Each slot resource is a file. Unrecognized
  12. resources are empty files, since every resource ID requires a special
  13. case (e.g. if the resource ID implies a directory or block, then its
  14. value has to be interpreted as a slot ROM pointer etc.).
  15. */
  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/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/byteorder.h>
  26. /*
  27. * /proc/bus/nubus/devices stuff
  28. */
  29. static int
  30. nubus_devices_proc_show(struct seq_file *m, void *v)
  31. {
  32. struct nubus_rsrc *fres;
  33. for_each_func_rsrc(fres)
  34. seq_printf(m, "%x\t%04x %04x %04x %04x\t%08lx\n",
  35. fres->board->slot, fres->category, fres->type,
  36. fres->dr_sw, fres->dr_hw, fres->board->slot_addr);
  37. return 0;
  38. }
  39. static struct proc_dir_entry *proc_bus_nubus_dir;
  40. /*
  41. * /proc/bus/nubus/x/ stuff
  42. */
  43. struct proc_dir_entry *nubus_proc_add_board(struct nubus_board *board)
  44. {
  45. char name[2];
  46. if (!proc_bus_nubus_dir || !nubus_populate_procfs)
  47. return NULL;
  48. snprintf(name, sizeof(name), "%x", board->slot);
  49. return proc_mkdir(name, proc_bus_nubus_dir);
  50. }
  51. /* The PDE private data for any directory under /proc/bus/nubus/x/
  52. * is the bytelanes value for the board in slot x.
  53. */
  54. struct proc_dir_entry *nubus_proc_add_rsrc_dir(struct proc_dir_entry *procdir,
  55. const struct nubus_dirent *ent,
  56. struct nubus_board *board)
  57. {
  58. char name[9];
  59. int lanes = board->lanes;
  60. if (!procdir || !nubus_populate_procfs)
  61. return NULL;
  62. snprintf(name, sizeof(name), "%x", ent->type);
  63. remove_proc_subtree(name, procdir);
  64. return proc_mkdir_data(name, 0555, procdir, (void *)lanes);
  65. }
  66. /* The PDE private data for a file under /proc/bus/nubus/x/ is a pointer to
  67. * an instance of the following structure, which gives the location and size
  68. * of the resource data in the slot ROM. For slot resources which hold only a
  69. * small integer, this integer value is stored directly and size is set to 0.
  70. * A NULL private data pointer indicates an unrecognized resource.
  71. */
  72. struct nubus_proc_pde_data {
  73. unsigned char *res_ptr;
  74. unsigned int res_size;
  75. };
  76. static struct nubus_proc_pde_data *
  77. nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size)
  78. {
  79. struct nubus_proc_pde_data *pded;
  80. pded = kmalloc(sizeof(*pded), GFP_KERNEL);
  81. if (!pded)
  82. return NULL;
  83. pded->res_ptr = ptr;
  84. pded->res_size = size;
  85. return pded;
  86. }
  87. static int nubus_proc_rsrc_show(struct seq_file *m, void *v)
  88. {
  89. struct inode *inode = m->private;
  90. struct nubus_proc_pde_data *pded;
  91. pded = pde_data(inode);
  92. if (!pded)
  93. return 0;
  94. if (pded->res_size > m->size)
  95. return -EFBIG;
  96. if (pded->res_size) {
  97. int lanes = (int)proc_get_parent_data(inode);
  98. struct nubus_dirent ent;
  99. if (!lanes)
  100. return 0;
  101. ent.mask = lanes;
  102. ent.base = pded->res_ptr;
  103. ent.data = 0;
  104. nubus_seq_write_rsrc_mem(m, &ent, pded->res_size);
  105. } else {
  106. unsigned int data = (unsigned int)pded->res_ptr;
  107. seq_putc(m, data >> 16);
  108. seq_putc(m, data >> 8);
  109. seq_putc(m, data >> 0);
  110. }
  111. return 0;
  112. }
  113. static int nubus_rsrc_proc_open(struct inode *inode, struct file *file)
  114. {
  115. return single_open(file, nubus_proc_rsrc_show, inode);
  116. }
  117. static const struct proc_ops nubus_rsrc_proc_ops = {
  118. .proc_open = nubus_rsrc_proc_open,
  119. .proc_read = seq_read,
  120. .proc_lseek = seq_lseek,
  121. .proc_release = single_release,
  122. };
  123. void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir,
  124. const struct nubus_dirent *ent,
  125. unsigned int size)
  126. {
  127. char name[9];
  128. struct nubus_proc_pde_data *pded;
  129. if (!procdir || !nubus_populate_procfs)
  130. return;
  131. snprintf(name, sizeof(name), "%x", ent->type);
  132. if (size)
  133. pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
  134. else
  135. pded = NULL;
  136. remove_proc_subtree(name, procdir);
  137. proc_create_data(name, S_IFREG | 0444, procdir,
  138. &nubus_rsrc_proc_ops, pded);
  139. }
  140. void nubus_proc_add_rsrc(struct proc_dir_entry *procdir,
  141. const struct nubus_dirent *ent)
  142. {
  143. char name[9];
  144. unsigned char *data = (unsigned char *)ent->data;
  145. if (!procdir || !nubus_populate_procfs)
  146. return;
  147. snprintf(name, sizeof(name), "%x", ent->type);
  148. remove_proc_subtree(name, procdir);
  149. proc_create_data(name, S_IFREG | 0444, procdir,
  150. &nubus_rsrc_proc_ops,
  151. nubus_proc_alloc_pde_data(data, 0));
  152. }
  153. /*
  154. * /proc/nubus stuff
  155. */
  156. void __init nubus_proc_init(void)
  157. {
  158. proc_create_single("nubus", 0, NULL, nubus_proc_show);
  159. proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
  160. if (!proc_bus_nubus_dir)
  161. return;
  162. proc_create_single("devices", 0, proc_bus_nubus_dir,
  163. nubus_devices_proc_show);
  164. }