blob: b8286ed65919304c8960488c2c0c3a457bbfcf40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Alexey Dobriyan076ec042008-04-29 01:01:54 -070024#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Adrian Bunk99ffab82008-02-04 22:30:23 -080026#include <linux/module.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
29#include <asm/byteorder.h>
30
31static int
Alexey Dobriyan076ec042008-04-29 01:01:54 -070032nubus_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct nubus_dev *dev = nubus_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Alexey Dobriyan076ec042008-04-29 01:01:54 -070036 while (dev) {
37 seq_printf(m, "%x\t%04x %04x %04x %04x",
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 dev->board->slot,
39 dev->category,
40 dev->type,
41 dev->dr_sw,
42 dev->dr_hw);
Alexey Dobriyan076ec042008-04-29 01:01:54 -070043 seq_printf(m, "\t%08lx\n", dev->board->slot_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 dev = dev->next;
45 }
Alexey Dobriyan076ec042008-04-29 01:01:54 -070046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Alexey Dobriyan076ec042008-04-29 01:01:54 -070049static 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 = {
Alexey Dobriyan076ec042008-04-29 01:01:54 -070055 .open = nubus_devices_proc_open,
56 .read = seq_read,
57 .llseek = seq_lseek,
58 .release = single_release,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static struct proc_dir_entry *proc_bus_nubus_dir;
62
David Howells11db6562013-04-10 15:05:38 +010063static const struct file_operations nubus_proc_subdir_fops = {
64#warning Need to set some I/O handlers here
65};
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static 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);
David Howells11db6562013-04-10 15:05:38 +010079 e = proc_create(name, S_IFREG | S_IRUGO | S_IWUSR, parent,
80 &nubus_proc_subdir_fops);
81 if (!e)
82 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
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}
Adrian Bunk99ffab82008-02-04 22:30:23 -0800148EXPORT_SYMBOL(nubus_proc_attach_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* FIXME: this is certainly broken! */
151int nubus_proc_detach_device(struct nubus_dev *dev)
152{
153 struct proc_dir_entry *e;
154
155 if ((e = dev->procdir)) {
156 if (atomic_read(&e->count))
157 return -EBUSY;
158 remove_proc_entry(e->name, proc_bus_nubus_dir);
159 dev->procdir = NULL;
160 }
161 return 0;
162}
Adrian Bunk99ffab82008-02-04 22:30:23 -0800163EXPORT_SYMBOL(nubus_proc_detach_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David Howells11db6562013-04-10 15:05:38 +0100165/*
166 * /proc/nubus stuff
167 */
168static int nubus_proc_show(struct seq_file *m, void *v)
169{
170 const struct nubus_board *board = v;
171
172 /* Display header on line 1 */
173 if (v == SEQ_START_TOKEN)
174 seq_puts(m, "Nubus devices found:\n");
175 else
176 seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
177 return 0;
178}
179
180static void *nubus_proc_start(struct seq_file *m, loff_t *_pos)
181{
182 struct nubus_board *board;
183 unsigned pos;
184
185 if (*_pos > LONG_MAX)
186 return NULL;
187 pos = *_pos;
188 if (pos == 0)
189 return SEQ_START_TOKEN;
190 for (board = nubus_boards; board; board = board->next)
191 if (--pos == 0)
192 break;
193 return board;
194}
195
196static void *nubus_proc_next(struct seq_file *p, void *v, loff_t *_pos)
197{
198 /* Walk the list of NuBus boards */
199 struct nubus_board *board = v;
200
201 ++*_pos;
202 if (v == SEQ_START_TOKEN)
203 board = nubus_boards;
204 else if (board)
205 board = board->next;
206 return board;
207}
208
209static void nubus_proc_stop(struct seq_file *p, void *v)
210{
211}
212
213static const struct seq_operations nubus_proc_seqops = {
214 .start = nubus_proc_start,
215 .next = nubus_proc_next,
216 .stop = nubus_proc_stop,
217 .show = nubus_proc_show,
218};
219
220static int nubus_proc_open(struct inode *inode, struct file *file)
221{
222 return seq_open(file, &nubus_proc_seqops);
223}
224
225static const struct file_operations nubus_proc_fops = {
226 .open = nubus_proc_open,
227 .read = seq_read,
228 .llseek = seq_lseek,
229 .release = seq_release,
230};
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232void __init proc_bus_nubus_add_devices(void)
233{
234 struct nubus_dev *dev;
235
236 for(dev = nubus_devices; dev; dev = dev->next)
237 nubus_proc_attach_device(dev);
238}
239
240void __init nubus_proc_init(void)
241{
David Howells11db6562013-04-10 15:05:38 +0100242 proc_create("nubus", 0, NULL, &nubus_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!MACH_IS_MAC)
244 return;
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700245 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
Alexey Dobriyan076ec042008-04-29 01:01:54 -0700246 proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 proc_bus_nubus_add_devices();
248}