Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/nubus.h> |
| 24 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 26 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
Adrian Bunk | 99ffab8 | 2008-02-04 22:30:23 -0800 | [diff] [blame] | 28 | #include <linux/module.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/byteorder.h> |
| 31 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 32 | /* |
| 33 | * /proc/bus/nubus/devices stuff |
| 34 | */ |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static int |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 37 | nubus_devices_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | struct nubus_dev *dev = nubus_devices; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 41 | while (dev) { |
| 42 | seq_printf(m, "%x\t%04x %04x %04x %04x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | dev->board->slot, |
| 44 | dev->category, |
| 45 | dev->type, |
| 46 | dev->dr_sw, |
| 47 | dev->dr_hw); |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 48 | seq_printf(m, "\t%08lx\n", dev->board->slot_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | dev = dev->next; |
| 50 | } |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 51 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 54 | static int nubus_devices_proc_open(struct inode *inode, struct file *file) |
| 55 | { |
| 56 | return single_open(file, nubus_devices_proc_show, NULL); |
| 57 | } |
| 58 | |
| 59 | static const struct file_operations nubus_devices_proc_fops = { |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 60 | .open = nubus_devices_proc_open, |
| 61 | .read = seq_read, |
| 62 | .llseek = seq_lseek, |
| 63 | .release = single_release, |
| 64 | }; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static struct proc_dir_entry *proc_bus_nubus_dir; |
| 67 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 68 | /* |
| 69 | * /proc/bus/nubus/x/ stuff |
| 70 | */ |
| 71 | |
| 72 | struct 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 | |
| 86 | struct 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 | |
| 106 | struct nubus_proc_pde_data { |
| 107 | unsigned char *res_ptr; |
| 108 | unsigned int res_size; |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 111 | static struct nubus_proc_pde_data * |
| 112 | nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 114 | struct nubus_proc_pde_data *pde_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 116 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 125 | static int nubus_proc_rsrc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 127 | struct inode *inode = m->private; |
| 128 | struct nubus_proc_pde_data *pde_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 130 | pde_data = PDE_DATA(inode); |
| 131 | if (!pde_data) |
Finn Thain | 6c8b89e | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 132 | return 0; |
| 133 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 134 | if (pde_data->res_size > m->size) |
| 135 | return -EFBIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 137 | if (pde_data->res_size) { |
| 138 | int lanes = (int)proc_get_parent_data(inode); |
| 139 | struct nubus_dirent ent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 141 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 157 | |
| 158 | static int nubus_proc_rsrc_open(struct inode *inode, struct file *file) |
| 159 | { |
| 160 | return single_open(file, nubus_proc_rsrc_show, inode); |
| 161 | } |
| 162 | |
| 163 | static 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 | |
| 170 | void 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 | |
| 189 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 204 | /* |
| 205 | * /proc/nubus stuff |
| 206 | */ |
| 207 | static 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 | |
| 219 | static 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 | |
| 235 | static 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 | |
| 248 | static void nubus_proc_stop(struct seq_file *p, void *v) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | static 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 | |
| 259 | static int nubus_proc_open(struct inode *inode, struct file *file) |
| 260 | { |
| 261 | return seq_open(file, &nubus_proc_seqops); |
| 262 | } |
| 263 | |
| 264 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | void __init nubus_proc_init(void) |
| 272 | { |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 273 | proc_create("nubus", 0, NULL, &nubus_proc_fops); |
Alexey Dobriyan | 9c37066 | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 274 | proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL); |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame^] | 275 | if (!proc_bus_nubus_dir) |
| 276 | return; |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 277 | proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |