blob: f47d90924ab4e6273cf05825aacd6e42f67c93d2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* 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
Finn Thain2f7dd072018-01-13 17:37:13 -050014 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/nubus.h>
24#include <linux/proc_fs.h>
Alexey Dobriyan076ec042008-04-29 01:01:54 -070025#include <linux/seq_file.h>
Finn Thain2f7dd072018-01-13 17:37:13 -050026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Adrian Bunk99ffab82008-02-04 22:30:23 -080028#include <linux/module.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/byteorder.h>
31
Finn Thain2f7dd072018-01-13 17:37:13 -050032/*
33 * /proc/bus/nubus/devices stuff
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int
Alexey Dobriyan076ec042008-04-29 01:01:54 -070037nubus_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct nubus_dev *dev = nubus_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alexey Dobriyan076ec042008-04-29 01:01:54 -070041 while (dev) {
42 seq_printf(m, "%x\t%04x %04x %04x %04x",
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 dev->board->slot,
44 dev->category,
45 dev->type,
46 dev->dr_sw,
47 dev->dr_hw);
Alexey Dobriyan076ec042008-04-29 01:01:54 -070048 seq_printf(m, "\t%08lx\n", dev->board->slot_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 dev = dev->next;
50 }
Alexey Dobriyan076ec042008-04-29 01:01:54 -070051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Alexey Dobriyan076ec042008-04-29 01:01:54 -070054static int nubus_devices_proc_open(struct inode *inode, struct file *file)
55{
56 return single_open(file, nubus_devices_proc_show, NULL);
57}
58
59static const struct file_operations nubus_devices_proc_fops = {
Alexey Dobriyan076ec042008-04-29 01:01:54 -070060 .open = nubus_devices_proc_open,
61 .read = seq_read,
62 .llseek = seq_lseek,
63 .release = single_release,
64};
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct proc_dir_entry *proc_bus_nubus_dir;
67
Finn Thain2f7dd072018-01-13 17:37:13 -050068/*
69 * /proc/bus/nubus/x/ stuff
70 */
71
72struct proc_dir_entry *nubus_proc_add_board(struct nubus_board *board)
73{
74 char name[2];
75
76 if (!proc_bus_nubus_dir)
77 return NULL;
78 snprintf(name, sizeof(name), "%x", board->slot);
79 return proc_mkdir(name, proc_bus_nubus_dir);
80}
81
82/* The PDE private data for any directory under /proc/bus/nubus/x/
83 * is the bytelanes value for the board in slot x.
84 */
85
86struct proc_dir_entry *nubus_proc_add_rsrc_dir(struct proc_dir_entry *procdir,
87 const struct nubus_dirent *ent,
88 struct nubus_board *board)
89{
90 char name[9];
91 int lanes = board->lanes;
92
93 if (!procdir)
94 return NULL;
95 snprintf(name, sizeof(name), "%x", ent->type);
96 return proc_mkdir_data(name, 0555, procdir, (void *)lanes);
97}
98
99/* The PDE private data for a file under /proc/bus/nubus/x/ is a pointer to
100 * an instance of the following structure, which gives the location and size
101 * of the resource data in the slot ROM. For slot resources which hold only a
102 * small integer, this integer value is stored directly and size is set to 0.
103 * A NULL private data pointer indicates an unrecognized resource.
104 */
105
106struct nubus_proc_pde_data {
107 unsigned char *res_ptr;
108 unsigned int res_size;
David Howells11db6562013-04-10 15:05:38 +0100109};
110
Finn Thain2f7dd072018-01-13 17:37:13 -0500111static struct nubus_proc_pde_data *
112nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Finn Thain2f7dd072018-01-13 17:37:13 -0500114 struct nubus_proc_pde_data *pde_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Finn Thain2f7dd072018-01-13 17:37:13 -0500116 pde_data = kmalloc(sizeof(*pde_data), GFP_KERNEL);
117 if (!pde_data)
118 return NULL;
119
120 pde_data->res_ptr = ptr;
121 pde_data->res_size = size;
122 return pde_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Finn Thain2f7dd072018-01-13 17:37:13 -0500125static int nubus_proc_rsrc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Finn Thain2f7dd072018-01-13 17:37:13 -0500127 struct inode *inode = m->private;
128 struct nubus_proc_pde_data *pde_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Finn Thain2f7dd072018-01-13 17:37:13 -0500130 pde_data = PDE_DATA(inode);
131 if (!pde_data)
Finn Thain6c8b89e2018-01-13 17:37:13 -0500132 return 0;
133
Finn Thain2f7dd072018-01-13 17:37:13 -0500134 if (pde_data->res_size > m->size)
135 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Finn Thain2f7dd072018-01-13 17:37:13 -0500137 if (pde_data->res_size) {
138 int lanes = (int)proc_get_parent_data(inode);
139 struct nubus_dirent ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Finn Thain2f7dd072018-01-13 17:37:13 -0500141 if (!lanes)
142 return 0;
143
144 ent.mask = lanes;
145 ent.base = pde_data->res_ptr;
146 ent.data = 0;
147 nubus_seq_write_rsrc_mem(m, &ent, pde_data->res_size);
148 } else {
149 unsigned int data = (unsigned int)pde_data->res_ptr;
150
151 seq_putc(m, data >> 16);
152 seq_putc(m, data >> 8);
153 seq_putc(m, data >> 0);
154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
Finn Thain2f7dd072018-01-13 17:37:13 -0500157
158static int nubus_proc_rsrc_open(struct inode *inode, struct file *file)
159{
160 return single_open(file, nubus_proc_rsrc_show, inode);
161}
162
163static const struct file_operations nubus_proc_rsrc_fops = {
164 .open = nubus_proc_rsrc_open,
165 .read = seq_read,
166 .llseek = seq_lseek,
167 .release = single_release,
168};
169
170void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir,
171 const struct nubus_dirent *ent,
172 unsigned int size)
173{
174 char name[9];
175 struct nubus_proc_pde_data *pde_data;
176
177 if (!procdir)
178 return;
179
180 snprintf(name, sizeof(name), "%x", ent->type);
181 if (size)
182 pde_data = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
183 else
184 pde_data = NULL;
185 proc_create_data(name, S_IFREG | 0444, procdir,
186 &nubus_proc_rsrc_fops, pde_data);
187}
188
189void nubus_proc_add_rsrc(struct proc_dir_entry *procdir,
190 const struct nubus_dirent *ent)
191{
192 char name[9];
193 unsigned char *data = (unsigned char *)ent->data;
194
195 if (!procdir)
196 return;
197
198 snprintf(name, sizeof(name), "%x", ent->type);
199 proc_create_data(name, S_IFREG | 0444, procdir,
200 &nubus_proc_rsrc_fops,
201 nubus_proc_alloc_pde_data(data, 0));
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David Howells11db6562013-04-10 15:05:38 +0100204/*
205 * /proc/nubus stuff
206 */
207static int nubus_proc_show(struct seq_file *m, void *v)
208{
209 const struct nubus_board *board = v;
210
211 /* Display header on line 1 */
212 if (v == SEQ_START_TOKEN)
213 seq_puts(m, "Nubus devices found:\n");
214 else
215 seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
216 return 0;
217}
218
219static void *nubus_proc_start(struct seq_file *m, loff_t *_pos)
220{
221 struct nubus_board *board;
222 unsigned pos;
223
224 if (*_pos > LONG_MAX)
225 return NULL;
226 pos = *_pos;
227 if (pos == 0)
228 return SEQ_START_TOKEN;
229 for (board = nubus_boards; board; board = board->next)
230 if (--pos == 0)
231 break;
232 return board;
233}
234
235static void *nubus_proc_next(struct seq_file *p, void *v, loff_t *_pos)
236{
237 /* Walk the list of NuBus boards */
238 struct nubus_board *board = v;
239
240 ++*_pos;
241 if (v == SEQ_START_TOKEN)
242 board = nubus_boards;
243 else if (board)
244 board = board->next;
245 return board;
246}
247
248static void nubus_proc_stop(struct seq_file *p, void *v)
249{
250}
251
252static const struct seq_operations nubus_proc_seqops = {
253 .start = nubus_proc_start,
254 .next = nubus_proc_next,
255 .stop = nubus_proc_stop,
256 .show = nubus_proc_show,
257};
258
259static int nubus_proc_open(struct inode *inode, struct file *file)
260{
261 return seq_open(file, &nubus_proc_seqops);
262}
263
264static const struct file_operations nubus_proc_fops = {
265 .open = nubus_proc_open,
266 .read = seq_read,
267 .llseek = seq_lseek,
268 .release = seq_release,
269};
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271void __init nubus_proc_init(void)
272{
David Howells11db6562013-04-10 15:05:38 +0100273 proc_create("nubus", 0, NULL, &nubus_proc_fops);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700274 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
Finn Thain2f7dd072018-01-13 17:37:13 -0500275 if (!proc_bus_nubus_dir)
276 return;
Alexey Dobriyan076ec042008-04-29 01:01:54 -0700277 proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}