Linux-libre 4.14.82-gnu
[librecmc/linux-libre.git] / drivers / nubus / proc.c
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 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>
25 #include <linux/seq_file.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28
29 #include <linux/uaccess.h>
30 #include <asm/byteorder.h>
31
32 static int
33 nubus_devices_proc_show(struct seq_file *m, void *v)
34 {
35         struct nubus_dev *dev = nubus_devices;
36
37         while (dev) {
38                 seq_printf(m, "%x\t%04x %04x %04x %04x",
39                               dev->board->slot,
40                               dev->category,
41                               dev->type,
42                               dev->dr_sw,
43                               dev->dr_hw);
44                 seq_printf(m, "\t%08lx\n", dev->board->slot_addr);
45                 dev = dev->next;
46         }
47         return 0;
48 }
49
50 static int nubus_devices_proc_open(struct inode *inode, struct file *file)
51 {
52         return single_open(file, nubus_devices_proc_show, NULL);
53 }
54
55 static const struct file_operations nubus_devices_proc_fops = {
56         .open           = nubus_devices_proc_open,
57         .read           = seq_read,
58         .llseek         = seq_lseek,
59         .release        = single_release,
60 };
61
62 static struct proc_dir_entry *proc_bus_nubus_dir;
63
64 static const struct file_operations nubus_proc_subdir_fops = {
65 #warning Need to set some I/O handlers here
66 };
67
68 static 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) {
76                 char name[8];
77                 struct proc_dir_entry* e;
78                 
79                 sprintf(name, "%x", ent.type);
80                 e = proc_create(name, S_IFREG | S_IRUGO | S_IWUSR, parent,
81                                 &nubus_proc_subdir_fops);
82                 if (!e)
83                         return;
84         }
85 }
86
87 /* Can't do this recursively since the root directory is structured
88    somewhat differently from the subdirectories */
89 static 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) {
98                 char name[8];
99                 struct proc_dir_entry* e;
100                 struct nubus_dir dir;
101                 
102                 sprintf(name, "%x", ent.type);
103                 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
118 int nubus_proc_attach_device(struct nubus_dev *dev)
119 {
120         struct proc_dir_entry *e;
121         struct nubus_dir root;
122         char name[8];
123
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                 
137         /* Create a directory */
138         sprintf(name, "%x", dev->board->slot);
139         e = dev->procdir = proc_mkdir(name, proc_bus_nubus_dir);
140         if (!e)
141                 return -ENOMEM;
142
143         /* Now recursively populate it with files */
144         nubus_get_root_dir(dev->board, &root);
145         nubus_proc_populate(dev, e, &root);
146
147         return 0;
148 }
149 EXPORT_SYMBOL(nubus_proc_attach_device);
150
151 /*
152  * /proc/nubus stuff
153  */
154 static int nubus_proc_show(struct seq_file *m, void *v)
155 {
156         const struct nubus_board *board = v;
157
158         /* Display header on line 1 */
159         if (v == SEQ_START_TOKEN)
160                 seq_puts(m, "Nubus devices found:\n");
161         else
162                 seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
163         return 0;
164 }
165
166 static void *nubus_proc_start(struct seq_file *m, loff_t *_pos)
167 {
168         struct nubus_board *board;
169         unsigned pos;
170
171         if (*_pos > LONG_MAX)
172                 return NULL;
173         pos = *_pos;
174         if (pos == 0)
175                 return SEQ_START_TOKEN;
176         for (board = nubus_boards; board; board = board->next)
177                 if (--pos == 0)
178                         break;
179         return board;
180 }
181
182 static void *nubus_proc_next(struct seq_file *p, void *v, loff_t *_pos)
183 {
184         /* Walk the list of NuBus boards */
185         struct nubus_board *board = v;
186
187         ++*_pos;
188         if (v == SEQ_START_TOKEN)
189                 board = nubus_boards;
190         else if (board)
191                 board = board->next;
192         return board;
193 }
194
195 static void nubus_proc_stop(struct seq_file *p, void *v)
196 {
197 }
198
199 static const struct seq_operations nubus_proc_seqops = {
200         .start  = nubus_proc_start,
201         .next   = nubus_proc_next,
202         .stop   = nubus_proc_stop,
203         .show   = nubus_proc_show,
204 };
205
206 static int nubus_proc_open(struct inode *inode, struct file *file)
207 {
208         return seq_open(file, &nubus_proc_seqops);
209 }
210
211 static const struct file_operations nubus_proc_fops = {
212         .open           = nubus_proc_open,
213         .read           = seq_read,
214         .llseek         = seq_lseek,
215         .release        = seq_release,
216 };
217
218 void __init proc_bus_nubus_add_devices(void)
219 {
220         struct nubus_dev *dev;
221         
222         for(dev = nubus_devices; dev; dev = dev->next)
223                 nubus_proc_attach_device(dev);
224 }
225
226 void __init nubus_proc_init(void)
227 {
228         proc_create("nubus", 0, NULL, &nubus_proc_fops);
229         if (!MACH_IS_MAC)
230                 return;
231         proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
232         proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
233         proc_bus_nubus_add_devices();
234 }