1 /*
2 * linux/fs/proc/scsi.c
3 * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
4 *
5 * The original version was derived from linux/fs/proc/net.c,
6 * which is Copyright (C) 1991, 1992 Linus Torvalds.
7 * Much has been rewritten, but some of the code still remains.
8 *
9 * /proc/scsi directory handling functions
10 *
11 * last change: 95/07/04
12 *
13 * Initial version: March '95
14 * 95/05/15 Added subdirectories for each driver and show every
15 * registered HBA as a single file.
16 * 95/05/30 Added rudimentary write support for parameter passing
17 * 95/07/04 Fixed bugs in directory handling
18 * 95/09/13 Update to support the new proc-dir tree
19 *
20 * TODO: Improve support to write to the driver files
21 * Add some more comments
22 */
23 #include <linux/autoconf.h>
24 #include <asm/segment.h>
25 #include <linux/errno.h>
26 #include <linux/sched.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stat.h>
29 #include <linux/mm.h>
30
31 /* forward references */
32 static int proc_readscsi(struct inode * inode, struct file * file,
33 char * buf, int count);
34 static int proc_writescsi(struct inode * inode, struct file * file,
35 const char * buf, int count);
36 static int proc_scsilseek(struct inode *, struct file *, off_t, int);
37
38 extern void build_proc_dir_hba_entries(uint);
39
40 /* the *_get_info() functions are in the respective scsi driver code */
41 extern int (* dispatch_scsi_info_ptr)(int, char *, char **, off_t, int, int);
42
43
44 static struct file_operations proc_scsi_operations = {
45 proc_scsilseek, /* lseek */
46 proc_readscsi, /* read */
47 proc_writescsi, /* write */
48 proc_readdir, /* readdir */
49 NULL, /* select */
50 NULL, /* ioctl */
51 NULL, /* mmap */
52 NULL, /* no special open code */
53 NULL, /* no special release code */
54 NULL /* can't fsync */
55 };
56
57 /*
58 * proc directories can do almost nothing..
59 */
60 struct inode_operations proc_scsi_inode_operations = {
61 &proc_scsi_operations, /* default scsi directory file-ops */
62 NULL, /* create */
63 proc_lookup, /* lookup */
64 NULL, /* link */
65 NULL, /* unlink */
66 NULL, /* symlink */
67 NULL, /* mkdir */
68 NULL, /* rmdir */
69 NULL, /* mknod */
70 NULL, /* rename */
71 NULL, /* readlink */
72 NULL, /* follow_link */
73 NULL, /* bmap */
74 NULL, /* truncate */
75 NULL /* permission */
76 };
77
78 int get_not_present_info(char *buffer, char **start, off_t offset, int length)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
79 {
80 int len, pos, begin;
81
82 begin = 0;
83 pos = len = sprintf(buffer,
84 "No low-level scsi modules are currently present\n");
85 if(pos < offset) {
86 len = 0;
87 begin = pos;
88 }
89
90 *start = buffer + (offset - begin); /* Start of wanted data */
91 len -= (offset - begin);
92 if(len > length)
93 len = length;
94
95 return(len);
96 }
97
98 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size, but our output routines
99 * use some slack for overruns
100 */
101
102 static int proc_readscsi(struct inode * inode, struct file * file,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
103 char * buf, int count)
104 {
105 int length;
106 int bytes = count;
107 int copied = 0;
108 int thistime;
109 char * page;
110 char * start;
111
112 if (count < -1) /* Normally I wouldn't do this, */
113 return(-EINVAL); /* but it saves some redundant code.
114 * Now it is possible to seek to the
115 * end of the file */
116 if (!(page = (char *) __get_free_page(GFP_KERNEL)))
117 return(-ENOMEM);
118
119 while(bytes > 0 || count == -1) {
120 thistime = bytes;
121 if(bytes > PROC_BLOCK_SIZE || count == -1)
122 thistime = PROC_BLOCK_SIZE;
123
124 if(dispatch_scsi_info_ptr)
125 length = dispatch_scsi_info_ptr(inode->i_ino, page, &start,
126 file->f_pos, thistime, 0);
127 else
128 length = get_not_present_info(page, &start, file->f_pos, thistime);
129 if(length < 0) {
130 free_page((ulong) page);
131 return(length);
132 }
133
134 /*
135 * We have been given a non page aligned block of
136 * the data we asked for + a bit. We have been given
137 * the start pointer and we know the length..
138 */
139 if (length <= 0)
140 break;
141 /*
142 * Copy the bytes, if we're not doing a seek to
143 * the end of the file
144 */
145 if (count != -1)
146 memcpy_tofs(buf + copied, start, length);
147 file->f_pos += length; /* Move down the file */
148 bytes -= length;
149 copied += length;
150
151 if(length < thistime)
152 break; /* End of file */
153
154 }
155
156 free_page((ulong) page);
157 return(copied);
158 }
159
160
161 static int proc_writescsi(struct inode * inode, struct file * file,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
162 const char * buf, int count)
163 {
164 int ret = 0;
165 char * page;
166
167 if(count > PROC_BLOCK_SIZE) {
168 return(-EOVERFLOW);
169 }
170
171 if(dispatch_scsi_info_ptr != NULL) {
172 if (!(page = (char *) __get_free_page(GFP_KERNEL)))
173 return(-ENOMEM);
174 memcpy_fromfs(page, buf, count);
175 ret = dispatch_scsi_info_ptr(inode->i_ino, page, 0, 0, count, 1);
176 } else
177 return(-ENOPKG); /* Nothing here */
178
179 free_page((ulong) page);
180 return(ret);
181 }
182
183
184 static int proc_scsilseek(struct inode * inode, struct file * file,
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
185 off_t offset, int orig)
186 {
187 switch (orig) {
188 case 0:
189 file->f_pos = offset;
190 return(file->f_pos);
191 case 1:
192 file->f_pos += offset;
193 return(file->f_pos);
194 case 2: /* This ugly hack allows us to */
195 if (offset) /* to determine the length of the */
196 return(-EINVAL); /* file and then later savely to */
197 proc_readscsi(inode, file, 0, -1); /* seek in it */
198 return(file->f_pos);
199 default:
200 return(-EINVAL);
201 }
202 }
203
204 /*
205 * Overrides for Emacs so that we almost follow Linus's tabbing style.
206 * Emacs will notice this stuff at the end of the file and automatically
207 * adjust the settings for this buffer only. This must remain at the end
208 * of the file.
209 * ---------------------------------------------------------------------------
210 * Local variables:
211 * c-indent-level: 4
212 * c-brace-imaginary-offset: 0
213 * c-brace-offset: -4
214 * c-argdecl-indent: 4
215 * c-label-offset: -4
216 * c-continued-statement-offset: 4
217 * c-continued-brace-offset: 0
218 * indent-tabs-mode: nil
219 * tab-width: 8
220 * End:
221 */