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)
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)
76 return NULL;
77 snprintf(name, sizeof(name), "%x", ent->type);
78 return proc_mkdir_data(name, 0555, procdir, (void *)lanes);
79}
80
81/* The PDE private data for a file under /proc/bus/nubus/x/ is a pointer to
82 * an instance of the following structure, which gives the location and size
83 * of the resource data in the slot ROM. For slot resources which hold only a
84 * small integer, this integer value is stored directly and size is set to 0.
85 * A NULL private data pointer indicates an unrecognized resource.
86 */
87
88struct nubus_proc_pde_data {
89 unsigned char *res_ptr;
90 unsigned int res_size;
91};
92
93static struct nubus_proc_pde_data *
94nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size)
95{
96 struct nubus_proc_pde_data *pded;
97
98 pded = kmalloc(sizeof(*pded), GFP_KERNEL);
99 if (!pded)
100 return NULL;
101
102 pded->res_ptr = ptr;
103 pded->res_size = size;
104 return pded;
105}
106
107static int nubus_proc_rsrc_show(struct seq_file *m, void *v)
108{
109 struct inode *inode = m->private;
110 struct nubus_proc_pde_data *pded;
111
112 pded = pde_data(inode);
113 if (!pded)
114 return 0;
115
116 if (pded->res_size > m->size)
117 return -EFBIG;
118
119 if (pded->res_size) {
120 int lanes = (int)proc_get_parent_data(inode);
121 struct nubus_dirent ent;
122
123 if (!lanes)
124 return 0;
125
126 ent.mask = lanes;
127 ent.base = pded->res_ptr;
128 ent.data = 0;
129 nubus_seq_write_rsrc_mem(m, &ent, pded->res_size);
130 } else {
131 unsigned int data = (unsigned int)pded->res_ptr;
132
133 seq_putc(m, data >> 16);
134 seq_putc(m, data >> 8);
135 seq_putc(m, data >> 0);
136 }
137 return 0;
138}
139
140void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir,
141 const struct nubus_dirent *ent,
142 unsigned int size)
143{
144 char name[9];
145 struct nubus_proc_pde_data *pded;
146
147 if (!procdir)
148 return;
149
150 snprintf(name, sizeof(name), "%x", ent->type);
151 if (size)
152 pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
153 else
154 pded = NULL;
155 proc_create_single_data(name, S_IFREG | 0444, procdir,
156 nubus_proc_rsrc_show, pded);
157}
158
159void nubus_proc_add_rsrc(struct proc_dir_entry *procdir,
160 const struct nubus_dirent *ent)
161{
162 char name[9];
163 unsigned char *data = (unsigned char *)ent->data;
164
165 if (!procdir)
166 return;
167
168 snprintf(name, sizeof(name), "%x", ent->type);
169 proc_create_single_data(name, S_IFREG | 0444, procdir,
170 nubus_proc_rsrc_show,
171 nubus_proc_alloc_pde_data(data, 0));
172}
173
174/*
175 * /proc/nubus stuff
176 */
177
178void __init nubus_proc_init(void)
179{
180 proc_create_single("nubus", 0, NULL, nubus_proc_show);
181 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
182 if (!proc_bus_nubus_dir)
183 return;
184 proc_create_single("devices", 0, proc_bus_nubus_dir,
185 nubus_devices_proc_show);
186}
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 .owner = THIS_MODULE,
56 .open = nubus_devices_proc_open,
57 .read = seq_read,
58 .llseek = seq_lseek,
59 .release = single_release,
60};
61
62static struct proc_dir_entry *proc_bus_nubus_dir;
63
64static void nubus_proc_subdir(struct nubus_dev* dev,
65 struct proc_dir_entry* parent,
66 struct nubus_dir* dir)
67{
68 struct nubus_dirent ent;
69
70 /* Some of these are directories, others aren't */
71 while (nubus_readdir(dir, &ent) != -1) {
72 char name[8];
73 struct proc_dir_entry* e;
74
75 sprintf(name, "%x", ent.type);
76 e = create_proc_entry(name, S_IFREG | S_IRUGO |
77 S_IWUSR, parent);
78 if (!e) return;
79 }
80}
81
82/* Can't do this recursively since the root directory is structured
83 somewhat differently from the subdirectories */
84static void nubus_proc_populate(struct nubus_dev* dev,
85 struct proc_dir_entry* parent,
86 struct nubus_dir* root)
87{
88 struct nubus_dirent ent;
89
90 /* We know these are all directories (board resource + one or
91 more functional resources) */
92 while (nubus_readdir(root, &ent) != -1) {
93 char name[8];
94 struct proc_dir_entry* e;
95 struct nubus_dir dir;
96
97 sprintf(name, "%x", ent.type);
98 e = proc_mkdir(name, parent);
99 if (!e) return;
100
101 /* And descend */
102 if (nubus_get_subdir(&ent, &dir) == -1) {
103 /* This shouldn't happen */
104 printk(KERN_ERR "NuBus root directory node %x:%x has no subdir!\n",
105 dev->board->slot, ent.type);
106 continue;
107 } else {
108 nubus_proc_subdir(dev, e, &dir);
109 }
110 }
111}
112
113int nubus_proc_attach_device(struct nubus_dev *dev)
114{
115 struct proc_dir_entry *e;
116 struct nubus_dir root;
117 char name[8];
118
119 if (dev == NULL) {
120 printk(KERN_ERR
121 "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
122 return -1;
123 }
124
125 if (dev->board == NULL) {
126 printk(KERN_ERR
127 "NULL pointer in nubus_proc_attach_device, shoot the programmer!\n");
128 printk("dev = %p, dev->board = %p\n", dev, dev->board);
129 return -1;
130 }
131
132 /* Create a directory */
133 sprintf(name, "%x", dev->board->slot);
134 e = dev->procdir = proc_mkdir(name, proc_bus_nubus_dir);
135 if (!e)
136 return -ENOMEM;
137
138 /* Now recursively populate it with files */
139 nubus_get_root_dir(dev->board, &root);
140 nubus_proc_populate(dev, e, &root);
141
142 return 0;
143}
144EXPORT_SYMBOL(nubus_proc_attach_device);
145
146/* FIXME: this is certainly broken! */
147int nubus_proc_detach_device(struct nubus_dev *dev)
148{
149 struct proc_dir_entry *e;
150
151 if ((e = dev->procdir)) {
152 if (atomic_read(&e->count))
153 return -EBUSY;
154 remove_proc_entry(e->name, proc_bus_nubus_dir);
155 dev->procdir = NULL;
156 }
157 return 0;
158}
159EXPORT_SYMBOL(nubus_proc_detach_device);
160
161void __init proc_bus_nubus_add_devices(void)
162{
163 struct nubus_dev *dev;
164
165 for(dev = nubus_devices; dev; dev = dev->next)
166 nubus_proc_attach_device(dev);
167}
168
169void __init nubus_proc_init(void)
170{
171 if (!MACH_IS_MAC)
172 return;
173 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
174 proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops);
175 proc_bus_nubus_add_devices();
176}