special usb hub handling, IDE disks, and retries all over the place
[linux-2.4.git] / drivers / nubus / proc.c
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/ptrace.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/nubus.h>
24 #include <linux/proc_fs.h>
25 #include <linux/init.h>
26 #include <asm/uaccess.h>
27 #include <asm/byteorder.h>
28
29 static int
30 get_nubus_dev_info(char *buf, char **start, off_t pos, int count)
31 {
32         struct nubus_dev *dev = nubus_devices;
33         off_t at = 0;
34         int len, cnt;
35
36         cnt = 0;
37         while (dev && count > cnt) {
38                 len = sprintf(buf, "%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                 len += sprintf(buf+len,
45                                "\t%08lx",
46                                dev->board->slot_addr);
47                 buf[len++] = '\n';
48                 at += len;
49                 if (at >= pos) {
50                         if (!*start) {
51                                 *start = buf + (pos - (at - len));
52                                 cnt = at - pos;
53                         } else
54                                 cnt += len;
55                         buf += len;
56                 }
57                 dev = dev->next;
58         }
59         return (count > cnt) ? cnt : count;
60 }
61
62 static struct proc_dir_entry *proc_bus_nubus_dir;
63
64 static 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 */
84 static 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
113 int 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 }
144
145 /* FIXME: this is certainly broken! */
146 int nubus_proc_detach_device(struct nubus_dev *dev)
147 {
148         struct proc_dir_entry *e;
149
150         if ((e = dev->procdir)) {
151                 if (atomic_read(&e->count))
152                         return -EBUSY;
153                 remove_proc_entry(e->name, proc_bus_nubus_dir);
154                 dev->procdir = NULL;
155         }
156         return 0;
157 }
158
159 void __init proc_bus_nubus_add_devices(void)
160 {
161         struct nubus_dev *dev;
162         
163         for(dev = nubus_devices; dev; dev = dev->next)
164                 nubus_proc_attach_device(dev);
165 }
166
167 void __init nubus_proc_init(void)
168 {
169         if (!MACH_IS_MAC)
170                 return;
171         proc_bus_nubus_dir = proc_mkdir("nubus", proc_bus);
172         create_proc_info_entry("devices", 0, proc_bus_nubus_dir,
173                                 get_nubus_dev_info);
174         proc_bus_nubus_add_devices();
175 }