pud_dev.c revision 1.7.2.3 1 /* $NetBSD: pud_dev.c,v 1.7.2.3 2016/07/19 06:26:59 pgoyette Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Research Foundation of Helsinki University of Technology
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: pud_dev.c,v 1.7.2.3 2016/07/19 06:26:59 pgoyette Exp $");
33
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/conf.h>
37 #include <sys/event.h>
38 #include <sys/ioccom.h>
39 #include <sys/kmem.h>
40 #include <sys/poll.h>
41 #include <sys/socketvar.h>
42 #include <sys/localcount.h>
43
44 #include <dev/pud/pud_sys.h>
45
46 /*
47 * b/c independent helpers
48 */
49
50 static int
51 doopenclose(dev_t dev, int flags, int fmt, int class, int type)
52 {
53 struct pud_req_openclose pc_oc; /* XXX: stack = stupid */
54
55 pc_oc.pm_flags = flags;
56 pc_oc.pm_fmt = fmt;
57
58 return pud_request(dev, &pc_oc, sizeof(pc_oc), class, type);
59 }
60
61 #include <sys/disklabel.h>
62 static int
63 doioctl(dev_t dev, u_long cmd, void *data, int flag, int class, int type)
64 {
65 struct pud_req_ioctl *pc_ioctl;
66 size_t dlen, allocsize;
67 int error;
68
69 dlen = IOCPARM_LEN(cmd);
70 allocsize = sizeof(struct pud_req_ioctl) + dlen;
71 pc_ioctl = kmem_zalloc(allocsize, KM_SLEEP);
72
73 pc_ioctl->pm_iocmd = cmd;
74 pc_ioctl->pm_flag = flag;
75
76 if (cmd & IOC_IN)
77 memcpy(pc_ioctl->pm_data, data, dlen);
78 error = pud_request(dev, pc_ioctl, allocsize, class, type);
79 if (error)
80 goto out;
81 if (cmd & IOC_OUT)
82 memcpy(data, pc_ioctl->pm_data, dlen);
83
84 out:
85 kmem_free(pc_ioctl, allocsize);
86 return error;
87 }
88
89 /*
90 * Block de-vices
91 */
92
93 static dev_type_open(pud_bdev_open);
94 static dev_type_close(pud_bdev_close);
95 static dev_type_strategy(pud_bdev_strategy);
96 static dev_type_ioctl(pud_bdev_ioctl);
97 #if 0
98 static dev_type_dump(pud_bdev_dump);
99 static dev_type_size(pud_bdev_size);
100 #endif
101
102 struct bdevsw pud_bdevsw = {
103 LOCALCOUNT_INITIALIZER
104 .d_open = pud_bdev_open,
105 .d_close = pud_bdev_close,
106 .d_strategy = pud_bdev_strategy,
107 .d_ioctl = pud_bdev_ioctl,
108 #if 0
109 .d_dump = pud_bdev_dump,
110 .d_psize = pud_bdev_size,
111 #endif
112 };
113
114 static int
115 pud_bdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
116 {
117
118 return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_OPEN);
119 }
120
121 static int
122 pud_bdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
123 {
124
125 return doopenclose(dev, flags, fmt, PUD_REQ_BDEV, PUD_BDEV_CLOSE);
126 }
127
128 static void
129 pud_bdev_strategy(struct buf *bp)
130 {
131 struct pud_req_readwrite *pc_rw;
132 size_t allocsize;
133 int error;
134
135 allocsize = sizeof(struct pud_req_readwrite) + bp->b_bcount;
136 pc_rw = kmem_zalloc(allocsize, KM_SLEEP);
137
138 pc_rw->pm_offset = bp->b_blkno << DEV_BSHIFT;
139 pc_rw->pm_resid = bp->b_bcount;
140
141 if (BUF_ISWRITE(bp))
142 memcpy(pc_rw->pm_data, bp->b_data, bp->b_bcount);
143
144 error = pud_request(bp->b_dev, pc_rw, allocsize, PUD_REQ_BDEV,
145 BUF_ISREAD(bp) ? PUD_BDEV_STRATREAD : PUD_BDEV_STRATWRITE);
146 if (error)
147 goto out;
148
149 if (pc_rw->pm_resid > bp->b_bcount) {
150 error = EINVAL;
151 goto out;
152 }
153
154 if (BUF_ISREAD(bp))
155 memcpy(bp->b_data,pc_rw->pm_data,bp->b_bcount-pc_rw->pm_resid);
156
157 bp->b_resid = pc_rw->pm_resid;
158
159 out:
160 kmem_free(pc_rw, allocsize);
161 bp->b_error = error;
162 biodone(bp);
163 }
164
165 int
166 pud_bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
167 {
168
169 return doioctl(dev, cmd, data, flag, PUD_REQ_BDEV, PUD_BDEV_IOCTL);
170 }
171
172 /* hnmmm */
173 #if 0
174 int
175 pud_bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
176 {
177
178 return EOPNOTSUPP;
179 }
180
181 int
182 pud_bdev_size(dev_t dev)
183 {
184
185 return 0;
186 }
187 #endif
188
189 /*
190 * Charrr devices
191 */
192
193 static dev_type_open(pud_cdev_open);
194 static dev_type_close(pud_cdev_close);
195 static dev_type_read(pud_cdev_read);
196 static dev_type_write(pud_cdev_write);
197 static dev_type_ioctl(pud_cdev_ioctl);
198 static dev_type_poll(pud_cdev_poll);
199 static dev_type_mmap(pud_cdev_mmap);
200 static dev_type_kqfilter(pud_cdev_kqfilter);
201
202 struct cdevsw pud_cdevsw = {
203 LOCALCOUNT_INITIALIZER
204 .d_open = pud_cdev_open,
205 .d_close = pud_cdev_close,
206 .d_read = pud_cdev_read,
207 .d_write = pud_cdev_write,
208 .d_ioctl = pud_cdev_ioctl,
209 #if 0
210 .d_stop = pud_cdev_stop,
211 .d_tty = pud_cdev_tty,
212 #endif
213 .d_poll = pud_cdev_poll,
214 .d_mmap = pud_cdev_mmap,
215 .d_kqfilter = pud_cdev_kqfilter,
216 .d_flag = D_OTHER,
217 };
218
219 static int
220 pud_cdev_open(dev_t dev, int flags, int fmt, lwp_t *l)
221 {
222
223 return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_OPEN);
224 }
225
226 static int
227 pud_cdev_close(dev_t dev, int flags, int fmt, lwp_t *l)
228 {
229
230 return doopenclose(dev, flags, fmt, PUD_REQ_CDEV, PUD_CDEV_CLOSE);
231 }
232
233 static int
234 pud_cdev_read(dev_t dev, struct uio *uio, int flag)
235 {
236 struct pud_creq_read *pc_read;
237 size_t allocsize;
238 int error;
239
240 allocsize = sizeof(struct pud_creq_read) + uio->uio_resid;
241 pc_read = kmem_zalloc(allocsize, KM_SLEEP);
242
243 pc_read->pm_offset = uio->uio_offset;
244 pc_read->pm_resid = uio->uio_resid;
245
246 error = pud_request(dev, pc_read, allocsize,
247 PUD_REQ_CDEV, PUD_CDEV_READ);
248 if (error)
249 goto out;
250
251 if (pc_read->pm_resid > uio->uio_resid) {
252 error = EINVAL;
253 goto out;
254 }
255
256 error = uiomove(pc_read->pm_data,
257 uio->uio_resid - pc_read->pm_resid, uio);
258
259 out:
260 kmem_free(pc_read, allocsize);
261 return error;
262 }
263
264 static int
265 pud_cdev_write(dev_t dev, struct uio *uio, int flag)
266 {
267 struct pud_creq_write *pc_write;
268 size_t allocsize;
269 int error;
270
271 allocsize = sizeof(struct pud_creq_write) + uio->uio_resid;
272 pc_write = kmem_zalloc(allocsize, KM_SLEEP);
273
274 pc_write->pm_offset = uio->uio_offset;
275 pc_write->pm_resid = uio->uio_resid;
276
277 error = uiomove(pc_write->pm_data, uio->uio_resid, uio);
278 if (error)
279 goto out;
280
281 error = pud_request(dev, pc_write, allocsize,
282 PUD_REQ_CDEV, PUD_CDEV_WRITE);
283 if (error)
284 goto out;
285
286 if (pc_write->pm_resid)
287 error = EIO;
288
289 out:
290 kmem_free(pc_write, allocsize);
291 return error;
292 }
293
294 static int
295 pud_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
296 {
297
298 return doioctl(dev, cmd, data, flag, PUD_REQ_CDEV, PUD_CDEV_IOCTL);
299 }
300
301 static paddr_t
302 pud_cdev_mmap(dev_t dev, off_t off, int flag)
303 {
304
305 return (paddr_t)-1;
306 }
307
308 static int
309 pud_cdev_poll(dev_t dev, int flag, lwp_t *l)
310 {
311
312 return EOPNOTSUPP;
313 }
314
315 static int
316 pud_cdev_kqfilter(dev_t dev, struct knote *kn)
317 {
318
319 return EOPNOTSUPP;
320 }
321