import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / drivers / zorro / proc.c
1 /*
2  *      $Id: proc.c,v 1.1.2.1 1998/06/07 23:21:01 geert Exp $
3  *
4  *      Procfs interface for the Zorro bus.
5  *
6  *      Copyright (C) 1998-2000 Geert Uytterhoeven
7  *
8  *      Heavily based on the procfs interface for the PCI bus, which is
9  *
10  *      Copyright (C) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
11  */
12
13 #include <linux/types.h>
14 #include <linux/zorro.h>
15 #include <linux/proc_fs.h>
16 #include <linux/init.h>
17 #include <asm/uaccess.h>
18 #include <asm/amigahw.h>
19 #include <asm/setup.h>
20
21 static loff_t
22 proc_bus_zorro_lseek(struct file *file, loff_t off, int whence)
23 {
24         loff_t new;
25
26         switch (whence) {
27         case 0:
28                 new = off;
29                 break;
30         case 1:
31                 new = file->f_pos + off;
32                 break;
33         case 2:
34                 new = sizeof(struct ConfigDev) + off;
35                 break;
36         default:
37                 return -EINVAL;
38         }
39         if (new < 0 || new > sizeof(struct ConfigDev))
40                 return -EINVAL;
41         return (file->f_pos = new);
42 }
43
44 static ssize_t
45 proc_bus_zorro_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
46 {
47         struct inode *ino = file->f_dentry->d_inode;
48         struct proc_dir_entry *dp = ino->u.generic_ip;
49         struct zorro_dev *dev = dp->data;
50         struct ConfigDev cd;
51         loff_t pos = *ppos;
52
53         if (pos < 0 || pos >= sizeof(struct ConfigDev))
54                 return 0;
55         if (nbytes > sizeof(struct ConfigDev) - pos)
56                 nbytes = sizeof(struct ConfigDev) - pos;
57
58         /* Construct a ConfigDev */
59         memset(&cd, 0, sizeof(cd));
60         cd.cd_Rom = dev->rom;
61         cd.cd_SlotAddr = dev->slotaddr;
62         cd.cd_SlotSize = dev->slotsize;
63         cd.cd_BoardAddr = (void *)dev->resource.start;
64         cd.cd_BoardSize = dev->resource.end-dev->resource.start+1;
65
66         if (copy_to_user(buf, &cd, nbytes))
67                 return -EFAULT;
68         *ppos = pos + nbytes;
69
70         return nbytes;
71 }
72
73 static struct file_operations proc_bus_zorro_operations = {
74         llseek:         proc_bus_zorro_lseek,
75         read:           proc_bus_zorro_read,
76 };
77
78 static int
79 get_zorro_dev_info(char *buf, char **start, off_t pos, int count)
80 {
81         u_int slot;
82         off_t at = 0;
83         int len, cnt;
84
85         for (slot = cnt = 0; slot < zorro_num_autocon && count > cnt; slot++) {
86                 struct zorro_dev *dev = &zorro_autocon[slot];
87                 len = sprintf(buf, "%02x\t%08x\t%08lx\t%08lx\t%02x\n", slot,
88                               dev->id, dev->resource.start,
89                               dev->resource.end-dev->resource.start+1,
90                               dev->rom.er_Type);
91                 at += len;
92                 if (at >= pos) {
93                         if (!*start) {
94                                 *start = buf + (pos - (at - len));
95                                 cnt = at - pos;
96                         } else
97                                 cnt += len;
98                         buf += len;
99                 }
100         }
101         return (count > cnt) ? cnt : count;
102 }
103
104 static struct proc_dir_entry *proc_bus_zorro_dir;
105
106 static int __init zorro_proc_attach_device(u_int slot)
107 {
108         struct proc_dir_entry *entry;
109         char name[4];
110
111         sprintf(name, "%02x", slot);
112         entry = create_proc_entry(name, 0, proc_bus_zorro_dir);
113         if (!entry)
114                 return -ENOMEM;
115         entry->proc_fops = &proc_bus_zorro_operations;
116         entry->data = &zorro_autocon[slot];
117         entry->size = sizeof(struct zorro_dev);
118         return 0;
119 }
120
121 static int __init zorro_proc_init(void)
122 {
123         u_int slot;
124
125         if (MACH_IS_AMIGA && AMIGAHW_PRESENT(ZORRO)) {
126                 proc_bus_zorro_dir = proc_mkdir("zorro", proc_bus);
127                 create_proc_info_entry("devices", 0, proc_bus_zorro_dir,
128                                        get_zorro_dev_info);
129                 for (slot = 0; slot < zorro_num_autocon; slot++)
130                         zorro_proc_attach_device(slot);
131         }
132         return 0;
133 }
134
135 __initcall(zorro_proc_init);