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