proc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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)
  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 *pde_data;
  79. pde_data = kmalloc(sizeof(*pde_data), GFP_KERNEL);
  80. if (!pde_data)
  81. return NULL;
  82. pde_data->res_ptr = ptr;
  83. pde_data->res_size = size;
  84. return pde_data;
  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 *pde_data;
  90. pde_data = PDE_DATA(inode);
  91. if (!pde_data)
  92. return 0;
  93. if (pde_data->res_size > m->size)
  94. return -EFBIG;
  95. if (pde_data->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 = pde_data->res_ptr;
  102. ent.data = 0;
  103. nubus_seq_write_rsrc_mem(m, &ent, pde_data->res_size);
  104. } else {
  105. unsigned int data = (unsigned int)pde_data->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. void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir,
  113. const struct nubus_dirent *ent,
  114. unsigned int size)
  115. {
  116. char name[9];
  117. struct nubus_proc_pde_data *pde_data;
  118. if (!procdir)
  119. return;
  120. snprintf(name, sizeof(name), "%x", ent->type);
  121. if (size)
  122. pde_data = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
  123. else
  124. pde_data = NULL;
  125. proc_create_single_data(name, S_IFREG | 0444, procdir,
  126. nubus_proc_rsrc_show, pde_data);
  127. }
  128. void nubus_proc_add_rsrc(struct proc_dir_entry *procdir,
  129. const struct nubus_dirent *ent)
  130. {
  131. char name[9];
  132. unsigned char *data = (unsigned char *)ent->data;
  133. if (!procdir)
  134. return;
  135. snprintf(name, sizeof(name), "%x", ent->type);
  136. proc_create_single_data(name, S_IFREG | 0444, procdir,
  137. nubus_proc_rsrc_show,
  138. nubus_proc_alloc_pde_data(data, 0));
  139. }
  140. /*
  141. * /proc/nubus stuff
  142. */
  143. void __init nubus_proc_init(void)
  144. {
  145. proc_create_single("nubus", 0, NULL, nubus_proc_show);
  146. proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
  147. if (!proc_bus_nubus_dir)
  148. return;
  149. proc_create_single("devices", 0, proc_bus_nubus_dir,
  150. nubus_devices_proc_show);
  151. }