proc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* drivers/nubus/proc.c: Proc FS interface for NuBus.
  3. By David Huggins-Daines <[email protected]>
  4. Much code and many ideas from drivers/pci/proc.c:
  5. Copyright (c) 1997, 1998 Martin Mares <[email protected]>
  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)
  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)
  61. return NULL;
  62. snprintf(name, sizeof(name), "%x", ent->type);
  63. return proc_mkdir_data(name, 0555, procdir, (void *)lanes);
  64. }
  65. /* The PDE private data for a file under /proc/bus/nubus/x/ is a pointer to
  66. * an instance of the following structure, which gives the location and size
  67. * of the resource data in the slot ROM. For slot resources which hold only a
  68. * small integer, this integer value is stored directly and size is set to 0.
  69. * A NULL private data pointer indicates an unrecognized resource.
  70. */
  71. struct nubus_proc_pde_data {
  72. unsigned char *res_ptr;
  73. unsigned int res_size;
  74. };
  75. static struct nubus_proc_pde_data *
  76. nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size)
  77. {
  78. struct nubus_proc_pde_data *pded;
  79. pded = kmalloc(sizeof(*pded), GFP_KERNEL);
  80. if (!pded)
  81. return NULL;
  82. pded->res_ptr = ptr;
  83. pded->res_size = size;
  84. return pded;
  85. }
  86. static int nubus_proc_rsrc_show(struct seq_file *m, void *v)
  87. {
  88. struct inode *inode = m->private;
  89. struct nubus_proc_pde_data *pded;
  90. pded = pde_data(inode);
  91. if (!pded)
  92. return 0;
  93. if (pded->res_size > m->size)
  94. return -EFBIG;
  95. if (pded->res_size) {
  96. int lanes = (int)proc_get_parent_data(inode);
  97. struct nubus_dirent ent;
  98. if (!lanes)
  99. return 0;
  100. ent.mask = lanes;
  101. ent.base = pded->res_ptr;
  102. ent.data = 0;
  103. nubus_seq_write_rsrc_mem(m, &ent, pded->res_size);
  104. } else {
  105. unsigned int data = (unsigned int)pded->res_ptr;
  106. seq_putc(m, data >> 16);
  107. seq_putc(m, data >> 8);
  108. seq_putc(m, data >> 0);
  109. }
  110. return 0;
  111. }
  112. static int nubus_rsrc_proc_open(struct inode *inode, struct file *file)
  113. {
  114. return single_open(file, nubus_proc_rsrc_show, inode);
  115. }
  116. static const struct proc_ops nubus_rsrc_proc_ops = {
  117. .proc_open = nubus_rsrc_proc_open,
  118. .proc_read = seq_read,
  119. .proc_lseek = seq_lseek,
  120. .proc_release = single_release,
  121. };
  122. void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir,
  123. const struct nubus_dirent *ent,
  124. unsigned int size)
  125. {
  126. char name[9];
  127. struct nubus_proc_pde_data *pded;
  128. if (!procdir)
  129. return;
  130. snprintf(name, sizeof(name), "%x", ent->type);
  131. if (size)
  132. pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
  133. else
  134. pded = NULL;
  135. proc_create_data(name, S_IFREG | 0444, procdir,
  136. &nubus_rsrc_proc_ops, pded);
  137. }
  138. void nubus_proc_add_rsrc(struct proc_dir_entry *procdir,
  139. const struct nubus_dirent *ent)
  140. {
  141. char name[9];
  142. unsigned char *data = (unsigned char *)ent->data;
  143. if (!procdir)
  144. return;
  145. snprintf(name, sizeof(name), "%x", ent->type);
  146. proc_create_data(name, S_IFREG | 0444, procdir,
  147. &nubus_rsrc_proc_ops,
  148. nubus_proc_alloc_pde_data(data, 0));
  149. }
  150. /*
  151. * /proc/nubus stuff
  152. */
  153. void __init nubus_proc_init(void)
  154. {
  155. proc_create_single("nubus", 0, NULL, nubus_proc_show);
  156. proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
  157. if (!proc_bus_nubus_dir)
  158. return;
  159. proc_create_single("devices", 0, proc_bus_nubus_dir,
  160. nubus_devices_proc_show);
  161. }