blob: 1fd667852271fd5495729f3782c97808b92e6fe8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* 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 Thain2f7dd072018-01-13 17:37:13 -050014 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 Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/nubus.h>
24#include <linux/proc_fs.h>
Alexey Dobriyan076ec042008-04-29 01:01:54 -070025#include <linux/seq_file.h>
Finn Thain2f7dd072018-01-13 17:37:13 -050026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Adrian Bunk99ffab82008-02-04 22:30:23 -080028#include <linux/module.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/byteorder.h>
31
Finn Thain2f7dd072018-01-13 17:37:13 -050032/*
33 * /proc/bus/nubus/devices stuff
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int
Alexey Dobriyan076ec042008-04-29 01:01:54 -070037nubus_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Finn Thain41b84812018-01-13 17:37:13 -050039 struct nubus_rsrc *fres;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Finn Thain41b84812018-01-13 17:37:13 -050041 for_each_func_rsrc(fres)
42 seq_printf(m, "%x\t%04x %04x %04x %04x\t%08lx\n",
Finn Thain189e19e2018-01-13 17:37:13 -050043 fres->board->slot, fres->category, fres->type,
Finn Thain41b84812018-01-13 17:37:13 -050044 fres->dr_sw, fres->dr_hw, fres->board->slot_addr);
Alexey Dobriyan076ec042008-04-29 01:01:54 -070045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48static struct proc_dir_entry *proc_bus_nubus_dir;
49
Finn Thain2f7dd072018-01-13 17:37:13 -050050/*
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;
David Howells11db6562013-04-10 15:05:38 +010091};
92
Finn Thain2f7dd072018-01-13 17:37:13 -050093static struct nubus_proc_pde_data *
94nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Muchun Song359745d2022-01-21 22:14:23 -080096 struct nubus_proc_pde_data *pded;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Muchun Song359745d2022-01-21 22:14:23 -080098 pded = kmalloc(sizeof(*pded), GFP_KERNEL);
99 if (!pded)
Finn Thain2f7dd072018-01-13 17:37:13 -0500100 return NULL;
101
Muchun Song359745d2022-01-21 22:14:23 -0800102 pded->res_ptr = ptr;
103 pded->res_size = size;
104 return pded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Finn Thain2f7dd072018-01-13 17:37:13 -0500107static int nubus_proc_rsrc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Finn Thain2f7dd072018-01-13 17:37:13 -0500109 struct inode *inode = m->private;
Muchun Song359745d2022-01-21 22:14:23 -0800110 struct nubus_proc_pde_data *pded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Muchun Song359745d2022-01-21 22:14:23 -0800112 pded = pde_data(inode);
113 if (!pded)
Finn Thain6c8b89e2018-01-13 17:37:13 -0500114 return 0;
115
Muchun Song359745d2022-01-21 22:14:23 -0800116 if (pded->res_size > m->size)
Finn Thain2f7dd072018-01-13 17:37:13 -0500117 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Muchun Song359745d2022-01-21 22:14:23 -0800119 if (pded->res_size) {
Finn Thain2f7dd072018-01-13 17:37:13 -0500120 int lanes = (int)proc_get_parent_data(inode);
121 struct nubus_dirent ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Finn Thain2f7dd072018-01-13 17:37:13 -0500123 if (!lanes)
124 return 0;
125
126 ent.mask = lanes;
Muchun Song359745d2022-01-21 22:14:23 -0800127 ent.base = pded->res_ptr;
Finn Thain2f7dd072018-01-13 17:37:13 -0500128 ent.data = 0;
Muchun Song359745d2022-01-21 22:14:23 -0800129 nubus_seq_write_rsrc_mem(m, &ent, pded->res_size);
Finn Thain2f7dd072018-01-13 17:37:13 -0500130 } else {
Muchun Song359745d2022-01-21 22:14:23 -0800131 unsigned int data = (unsigned int)pded->res_ptr;
Finn Thain2f7dd072018-01-13 17:37:13 -0500132
133 seq_putc(m, data >> 16);
134 seq_putc(m, data >> 8);
135 seq_putc(m, data >> 0);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138}
Finn Thain2f7dd072018-01-13 17:37:13 -0500139
Finn Thain2f7dd072018-01-13 17:37:13 -0500140void 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];
Muchun Song359745d2022-01-21 22:14:23 -0800145 struct nubus_proc_pde_data *pded;
Finn Thain2f7dd072018-01-13 17:37:13 -0500146
147 if (!procdir)
148 return;
149
150 snprintf(name, sizeof(name), "%x", ent->type);
151 if (size)
Muchun Song359745d2022-01-21 22:14:23 -0800152 pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size);
Finn Thain2f7dd072018-01-13 17:37:13 -0500153 else
Muchun Song359745d2022-01-21 22:14:23 -0800154 pded = NULL;
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200155 proc_create_single_data(name, S_IFREG | 0444, procdir,
Muchun Song359745d2022-01-21 22:14:23 -0800156 nubus_proc_rsrc_show, pded);
Finn Thain2f7dd072018-01-13 17:37:13 -0500157}
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);
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200169 proc_create_single_data(name, S_IFREG | 0444, procdir,
170 nubus_proc_rsrc_show,
171 nubus_proc_alloc_pde_data(data, 0));
Finn Thain2f7dd072018-01-13 17:37:13 -0500172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Howells11db6562013-04-10 15:05:38 +0100174/*
175 * /proc/nubus stuff
176 */
David Howells11db6562013-04-10 15:05:38 +0100177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void __init nubus_proc_init(void)
179{
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200180 proc_create_single("nubus", 0, NULL, nubus_proc_show);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700181 proc_bus_nubus_dir = proc_mkdir("bus/nubus", NULL);
Finn Thain2f7dd072018-01-13 17:37:13 -0500182 if (!proc_bus_nubus_dir)
183 return;
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200184 proc_create_single("devices", 0, proc_bus_nubus_dir,
185 nubus_devices_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}