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 | { |
Finn Thain | 41b8481 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 39 | struct nubus_rsrc *fres; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Finn Thain | 41b8481 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 41 | for_each_func_rsrc(fres) |
| 42 | seq_printf(m, "%x\t%04x %04x %04x %04x\t%08lx\n", |
Finn Thain | 189e19e | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 43 | fres->board->slot, fres->category, fres->type, |
Finn Thain | 41b8481 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 44 | fres->dr_sw, fres->dr_hw, fres->board->slot_addr); |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 45 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 48 | static int nubus_devices_proc_open(struct inode *inode, struct file *file) |
| 49 | { |
| 50 | return single_open(file, nubus_devices_proc_show, NULL); |
| 51 | } |
| 52 | |
| 53 | static const struct file_operations nubus_devices_proc_fops = { |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 54 | .open = nubus_devices_proc_open, |
| 55 | .read = seq_read, |
| 56 | .llseek = seq_lseek, |
| 57 | .release = single_release, |
| 58 | }; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static struct proc_dir_entry *proc_bus_nubus_dir; |
| 61 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 62 | /* |
| 63 | * /proc/bus/nubus/x/ stuff |
| 64 | */ |
| 65 | |
| 66 | struct proc_dir_entry *nubus_proc_add_board(struct nubus_board *board) |
| 67 | { |
| 68 | char name[2]; |
| 69 | |
| 70 | if (!proc_bus_nubus_dir) |
| 71 | return NULL; |
| 72 | snprintf(name, sizeof(name), "%x", board->slot); |
| 73 | return proc_mkdir(name, proc_bus_nubus_dir); |
| 74 | } |
| 75 | |
| 76 | /* The PDE private data for any directory under /proc/bus/nubus/x/ |
| 77 | * is the bytelanes value for the board in slot x. |
| 78 | */ |
| 79 | |
| 80 | struct proc_dir_entry *nubus_proc_add_rsrc_dir(struct proc_dir_entry *procdir, |
| 81 | const struct nubus_dirent *ent, |
| 82 | struct nubus_board *board) |
| 83 | { |
| 84 | char name[9]; |
| 85 | int lanes = board->lanes; |
| 86 | |
| 87 | if (!procdir) |
| 88 | return NULL; |
| 89 | snprintf(name, sizeof(name), "%x", ent->type); |
| 90 | return proc_mkdir_data(name, 0555, procdir, (void *)lanes); |
| 91 | } |
| 92 | |
| 93 | /* The PDE private data for a file under /proc/bus/nubus/x/ is a pointer to |
| 94 | * an instance of the following structure, which gives the location and size |
| 95 | * of the resource data in the slot ROM. For slot resources which hold only a |
| 96 | * small integer, this integer value is stored directly and size is set to 0. |
| 97 | * A NULL private data pointer indicates an unrecognized resource. |
| 98 | */ |
| 99 | |
| 100 | struct nubus_proc_pde_data { |
| 101 | unsigned char *res_ptr; |
| 102 | unsigned int res_size; |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 105 | static struct nubus_proc_pde_data * |
| 106 | nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 108 | struct nubus_proc_pde_data *pde_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 110 | pde_data = kmalloc(sizeof(*pde_data), GFP_KERNEL); |
| 111 | if (!pde_data) |
| 112 | return NULL; |
| 113 | |
| 114 | pde_data->res_ptr = ptr; |
| 115 | pde_data->res_size = size; |
| 116 | return pde_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 119 | static int nubus_proc_rsrc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 121 | struct inode *inode = m->private; |
| 122 | struct nubus_proc_pde_data *pde_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 124 | pde_data = PDE_DATA(inode); |
| 125 | if (!pde_data) |
Finn Thain | 6c8b89e | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 126 | return 0; |
| 127 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 128 | if (pde_data->res_size > m->size) |
| 129 | return -EFBIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 131 | if (pde_data->res_size) { |
| 132 | int lanes = (int)proc_get_parent_data(inode); |
| 133 | struct nubus_dirent ent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 135 | if (!lanes) |
| 136 | return 0; |
| 137 | |
| 138 | ent.mask = lanes; |
| 139 | ent.base = pde_data->res_ptr; |
| 140 | ent.data = 0; |
| 141 | nubus_seq_write_rsrc_mem(m, &ent, pde_data->res_size); |
| 142 | } else { |
| 143 | unsigned int data = (unsigned int)pde_data->res_ptr; |
| 144 | |
| 145 | seq_putc(m, data >> 16); |
| 146 | seq_putc(m, data >> 8); |
| 147 | seq_putc(m, data >> 0); |
| 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 151 | |
| 152 | static int nubus_proc_rsrc_open(struct inode *inode, struct file *file) |
| 153 | { |
| 154 | return single_open(file, nubus_proc_rsrc_show, inode); |
| 155 | } |
| 156 | |
| 157 | static const struct file_operations nubus_proc_rsrc_fops = { |
| 158 | .open = nubus_proc_rsrc_open, |
| 159 | .read = seq_read, |
| 160 | .llseek = seq_lseek, |
| 161 | .release = single_release, |
| 162 | }; |
| 163 | |
| 164 | void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir, |
| 165 | const struct nubus_dirent *ent, |
| 166 | unsigned int size) |
| 167 | { |
| 168 | char name[9]; |
| 169 | struct nubus_proc_pde_data *pde_data; |
| 170 | |
| 171 | if (!procdir) |
| 172 | return; |
| 173 | |
| 174 | snprintf(name, sizeof(name), "%x", ent->type); |
| 175 | if (size) |
| 176 | pde_data = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size); |
| 177 | else |
| 178 | pde_data = NULL; |
| 179 | proc_create_data(name, S_IFREG | 0444, procdir, |
| 180 | &nubus_proc_rsrc_fops, pde_data); |
| 181 | } |
| 182 | |
| 183 | void nubus_proc_add_rsrc(struct proc_dir_entry *procdir, |
| 184 | const struct nubus_dirent *ent) |
| 185 | { |
| 186 | char name[9]; |
| 187 | unsigned char *data = (unsigned char *)ent->data; |
| 188 | |
| 189 | if (!procdir) |
| 190 | return; |
| 191 | |
| 192 | snprintf(name, sizeof(name), "%x", ent->type); |
| 193 | proc_create_data(name, S_IFREG | 0444, procdir, |
| 194 | &nubus_proc_rsrc_fops, |
| 195 | nubus_proc_alloc_pde_data(data, 0)); |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 198 | /* |
| 199 | * /proc/nubus stuff |
| 200 | */ |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 201 | |
| 202 | static int nubus_proc_open(struct inode *inode, struct file *file) |
| 203 | { |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 204 | return single_open(file, nubus_proc_show, NULL); |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static const struct file_operations nubus_proc_fops = { |
| 208 | .open = nubus_proc_open, |
| 209 | .read = seq_read, |
| 210 | .llseek = seq_lseek, |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 211 | .release = single_release, |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 212 | }; |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | void __init nubus_proc_init(void) |
| 215 | { |
David Howells | 11db656 | 2013-04-10 15:05:38 +0100 | [diff] [blame] | 216 | proc_create("nubus", 0, NULL, &nubus_proc_fops); |
Alexey Dobriyan | 9c37066 | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 217 | proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL); |
Finn Thain | 2f7dd07 | 2018-01-13 17:37:13 -0500 | [diff] [blame] | 218 | if (!proc_bus_nubus_dir) |
| 219 | return; |
Alexey Dobriyan | 076ec04 | 2008-04-29 01:01:54 -0700 | [diff] [blame] | 220 | proc_create("devices", 0, proc_bus_nubus_dir, &nubus_devices_proc_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |