blob: 91211192f36f2b4f5eaaa53e1fb2998a2a4dfaae [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
14 Therefore each NuBus device is in fact a directory, which may in
15 turn contain subdirectories. The "files" correspond to NuBus
16 resource records. For those types of records which we know how to
17 convert to formats that are meaningful to userspace (mostly just
18 icons) these files will provide "cooked" data. Otherwise they will
19 simply provide raw access (read-only of course) to the ROM. */
20
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Adrian Bunk99ffab82008-02-04 22:30:23 -080027#include <linux/module.h>
28
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
32static int
Alexey Dobriyan076ec042008-04-29 01:01:54 -070033nubus_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct nubus_dev *dev = nubus_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Alexey Dobriyan076ec042008-04-29 01:01:54 -070037 while (dev) {
38 seq_printf(m, "%x\t%04x %04x %04x %04x",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 dev->board->slot,
40 dev->category,
41 dev->type,
42 dev->dr_sw,
43 dev->dr_hw);
Alexey Dobriyan076ec042008-04-29 01:01:54 -070044 seq_printf(m, "\t%08lx\n", dev->board->slot_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 dev = dev->next;
46 }
Alexey Dobriyan076ec042008-04-29 01:01:54 -070047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Alexey Dobriyan076ec042008-04-29 01:01:54 -070050static int nubus_devices_proc_open(struct inode *inode, struct file *file)
51{
52 return single_open(file, nubus_devices_proc_show, NULL);
53}
54
55static const struct file_operations nubus_devices_proc_fops = {
Alexey Dobriyan076ec042008-04-29 01:01:54 -070056 .open = nubus_devices_proc_open,
57 .read = seq_read,
58 .llseek = seq_lseek,
59 .release = single_release,
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct proc_dir_entry *proc_bus_nubus_dir;
63
David Howells11db6562013-04-10 15:05:38 +010064static const struct file_operations nubus_proc_subdir_fops = {
65#warning Need to set some I/O handlers here
66};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void nubus_proc_subdir(struct nubus_dev* dev,
69 struct proc_dir_entry* parent,
70 struct nubus_dir* dir)
71{
72 struct nubus_dirent ent;
73
74 /* Some of these are directories, others aren't */
75 while (nubus_readdir(dir, &ent) != -1) {
Finn Thain2f828fb2018-01-13 17:37:13 -050076 char name[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct proc_dir_entry* e;
78
Finn Thain2f828fb2018-01-13 17:37:13 -050079 snprintf(name, sizeof(name), "%x", ent.type);
David Howells11db6562013-04-10 15:05:38 +010080 e = proc_create(name, S_IFREG | S_IRUGO | S_IWUSR, parent,
81 &nubus_proc_subdir_fops);
82 if (!e)
83 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85}
86
87/* Can't do this recursively since the root directory is structured
88 somewhat differently from the subdirectories */
89static void nubus_proc_populate(struct nubus_dev* dev,
90 struct proc_dir_entry* parent,
91 struct nubus_dir* root)
92{
93 struct nubus_dirent ent;
94
95 /* We know these are all directories (board resource + one or
96 more functional resources) */
97 while (nubus_readdir(root, &ent) != -1) {
Finn Thain2f828fb2018-01-13 17:37:13 -050098 char name[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct proc_dir_entry* e;
100 struct nubus_dir dir;
101
Finn Thain2f828fb2018-01-13 17:37:13 -0500102 snprintf(name, sizeof(name), "%x", ent.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 e = proc_mkdir(name, parent);
104 if (!e) return;
105
106 /* And descend */
107 if (nubus_get_subdir(&ent, &dir) == -1) {
108 /* This shouldn't happen */
109 printk(KERN_ERR "NuBus root directory node %x:%x has no subdir!\n",
110 dev->board->slot, ent.type);
111 continue;
112 } else {
113 nubus_proc_subdir(dev, e, &dir);
114 }
115 }
116}
117
118int nubus_proc_attach_device(struct nubus_dev *dev)
119{
120 struct proc_dir_entry *e;
121 struct nubus_dir root;
Finn Thain2f828fb2018-01-13 17:37:13 -0500122 char name[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (dev == NULL) {
125 printk(KERN_ERR
126 "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
127 return -1;
128 }
129
130 if (dev->board == NULL) {
131 printk(KERN_ERR
132 "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
133 printk("dev = %p, dev->board = %p\n", dev, dev->board);
134 return -1;
135 }
136
Finn Thain6c8b89e2018-01-13 17:37:13 -0500137 if (dev->board->procdir)
138 return 0;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Create a directory */
Finn Thain2f828fb2018-01-13 17:37:13 -0500141 snprintf(name, sizeof(name), "%x", dev->board->slot);
Finn Thain6c8b89e2018-01-13 17:37:13 -0500142 e = proc_mkdir(name, proc_bus_nubus_dir);
143 dev->board->procdir = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!e)
145 return -ENOMEM;
146
147 /* Now recursively populate it with files */
148 nubus_get_root_dir(dev->board, &root);
149 nubus_proc_populate(dev, e, &root);
150
151 return 0;
152}
Adrian Bunk99ffab82008-02-04 22:30:23 -0800153EXPORT_SYMBOL(nubus_proc_attach_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
David Howells11db6562013-04-10 15:05:38 +0100155/*
156 * /proc/nubus stuff
157 */
158static int nubus_proc_show(struct seq_file *m, void *v)
159{
160 const struct nubus_board *board = v;
161
162 /* Display header on line 1 */
163 if (v == SEQ_START_TOKEN)
164 seq_puts(m, "Nubus devices found:\n");
165 else
166 seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
167 return 0;
168}
169
170static void *nubus_proc_start(struct seq_file *m, loff_t *_pos)
171{
172 struct nubus_board *board;
173 unsigned pos;
174
175 if (*_pos > LONG_MAX)
176 return NULL;
177 pos = *_pos;
178 if (pos == 0)
179 return SEQ_START_TOKEN;
180 for (board = nubus_boards; board; board = board->next)
181 if (--pos == 0)
182 break;
183 return board;
184}
185
186static void *nubus_proc_next(struct seq_file *p, void *v, loff_t *_pos)
187{
188 /* Walk the list of NuBus boards */
189 struct nubus_board *board = v;
190
191 ++*_pos;
192 if (v == SEQ_START_TOKEN)
193 board = nubus_boards;
194 else if (board)
195 board = board->next;
196 return board;
197}
198
199static void nubus_proc_stop(struct seq_file *p, void *v)
200{
201}
202
203static const struct seq_operations nubus_proc_seqops = {
204 .start = nubus_proc_start,
205 .next = nubus_proc_next,
206 .stop = nubus_proc_stop,
207 .show = nubus_proc_show,
208};
209
210static int nubus_proc_open(struct inode *inode, struct file *file)
211{
212 return seq_open(file, &nubus_proc_seqops);
213}
214
215static const struct file_operations nubus_proc_fops = {
216 .open = nubus_proc_open,
217 .read = seq_read,
218 .llseek = seq_lseek,
219 .release = seq_release,
220};
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222void __init proc_bus_nubus_add_devices(void)
223{
224 struct nubus_dev *dev;
225
226 for(dev = nubus_devices; dev; dev = dev->next)
227 nubus_proc_attach_device(dev);
228}
229
230void __init nubus_proc_init(void)
231{
David Howells11db6562013-04-10 15:05:38 +0100232 proc_create("nubus", 0, NULL, &nubus_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!MACH_IS_MAC)
234 return;
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700235 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
Alexey Dobriyan076ec042008-04-29 01:01:54 -0700236 proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 proc_bus_nubus_add_devices();
238}