altmem.c revision 1.2 1 1.2 dholland /* $NetBSD: altmem.c,v 1.2 2014/03/16 05:20:26 dholland Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill #include <sys/cdefs.h>
29 1.2 dholland __KERNEL_RCSID(0, "$NetBSD: altmem.c,v 1.2 2014/03/16 05:20:26 dholland Exp $");
30 1.1 jmcneill #include <sys/param.h>
31 1.1 jmcneill #include <sys/types.h>
32 1.1 jmcneill #include <sys/device.h>
33 1.1 jmcneill #include <sys/conf.h>
34 1.1 jmcneill #include <sys/buf.h>
35 1.1 jmcneill #include <sys/disklabel.h>
36 1.1 jmcneill #include <sys/disk.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/altmem/altmemvar.h>
39 1.1 jmcneill
40 1.1 jmcneill struct altmem_softc {
41 1.1 jmcneill device_t sc_dev;
42 1.1 jmcneill
43 1.1 jmcneill struct disk sc_dkdev;
44 1.1 jmcneill
45 1.1 jmcneill void *sc_cookie;
46 1.1 jmcneill const struct altmem_memops *sc_memops;
47 1.1 jmcneill
48 1.1 jmcneill size_t sc_size;
49 1.1 jmcneill };
50 1.1 jmcneill
51 1.1 jmcneill static dev_type_open(altmemopen);
52 1.1 jmcneill static dev_type_close(altmemclose);
53 1.1 jmcneill static dev_type_read(altmemread);
54 1.1 jmcneill static dev_type_write(altmemwrite);
55 1.1 jmcneill static dev_type_ioctl(altmemioctl);
56 1.1 jmcneill static dev_type_strategy(altmemstrategy);
57 1.1 jmcneill static dev_type_size(altmemsize);
58 1.1 jmcneill
59 1.1 jmcneill static int altmem_match(device_t, cfdata_t, void *);
60 1.1 jmcneill static void altmem_attach(device_t, device_t, void *);
61 1.1 jmcneill
62 1.1 jmcneill const struct bdevsw altmem_bdevsw = {
63 1.2 dholland .d_open = altmemopen,
64 1.2 dholland .d_close = altmemclose,
65 1.2 dholland .d_strategy = altmemstrategy,
66 1.2 dholland .d_ioctl = altmemioctl,
67 1.2 dholland .d_dump = nodump,
68 1.2 dholland .d_psize = altmemsize,
69 1.2 dholland .d_flag = D_DISK
70 1.1 jmcneill };
71 1.1 jmcneill const struct cdevsw altmem_cdevsw = {
72 1.2 dholland .d_open = altmemopen,
73 1.2 dholland .d_close = altmemclose,
74 1.2 dholland .d_read = altmemread,
75 1.2 dholland .d_write = altmemwrite,
76 1.2 dholland .d_ioctl = altmemioctl,
77 1.2 dholland .d_stop = nostop,
78 1.2 dholland .d_tty = notty,
79 1.2 dholland .d_poll = nopoll,
80 1.2 dholland .d_mmap = nommap,
81 1.2 dholland .d_kqfilter = nokqfilter,
82 1.2 dholland .d_flag = D_DISK
83 1.1 jmcneill };
84 1.1 jmcneill static struct dkdriver altmemdkdriver = { altmemstrategy, minphys };
85 1.1 jmcneill extern struct cfdriver altmem_cd;
86 1.1 jmcneill
87 1.1 jmcneill CFATTACH_DECL_NEW(altmem, sizeof(struct altmem_softc), altmem_match,
88 1.1 jmcneill altmem_attach, NULL, NULL);
89 1.1 jmcneill
90 1.1 jmcneill static int
91 1.1 jmcneill altmem_match(device_t parent, cfdata_t match, void *opaque)
92 1.1 jmcneill {
93 1.1 jmcneill return 1;
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill static void
97 1.1 jmcneill altmem_attach(device_t parent, device_t self, void *opaque)
98 1.1 jmcneill {
99 1.1 jmcneill struct altmem_softc *sc = device_private(self);
100 1.1 jmcneill struct altmem_attach_args *aaa = opaque;
101 1.1 jmcneill char pbuf[9];
102 1.1 jmcneill
103 1.1 jmcneill sc->sc_dev = self;
104 1.1 jmcneill sc->sc_cookie = aaa->cookie;
105 1.1 jmcneill sc->sc_memops = aaa->memops;
106 1.1 jmcneill sc->sc_size = sc->sc_memops->getsize(sc->sc_cookie);
107 1.1 jmcneill
108 1.1 jmcneill format_bytes(pbuf, sizeof(pbuf), sc->sc_size);
109 1.1 jmcneill
110 1.1 jmcneill aprint_naive("\n");
111 1.1 jmcneill aprint_normal(": %s\n", pbuf);
112 1.1 jmcneill
113 1.1 jmcneill disk_init(&sc->sc_dkdev, device_xname(self), &altmemdkdriver);
114 1.1 jmcneill disk_attach(&sc->sc_dkdev);
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill static int
118 1.1 jmcneill altmemsize(dev_t dev)
119 1.1 jmcneill {
120 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
121 1.1 jmcneill if (sc == NULL)
122 1.1 jmcneill return 0;
123 1.1 jmcneill return sc->sc_size >> DEV_BSHIFT;
124 1.1 jmcneill }
125 1.1 jmcneill
126 1.1 jmcneill static int
127 1.1 jmcneill altmemopen(dev_t dev, int flag, int fmt, struct lwp *l)
128 1.1 jmcneill {
129 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
130 1.1 jmcneill if (sc == NULL)
131 1.1 jmcneill return ENXIO;
132 1.1 jmcneill return 0;
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static int
136 1.1 jmcneill altmemclose(dev_t dev, int flag, int fmt, struct lwp *l)
137 1.1 jmcneill {
138 1.1 jmcneill return 0;
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static int
142 1.1 jmcneill altmemread(dev_t dev, struct uio *uio, int flags)
143 1.1 jmcneill {
144 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL)
145 1.1 jmcneill return ENXIO;
146 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_READ, minphys, uio);
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill static int
150 1.1 jmcneill altmemwrite(dev_t dev, struct uio *uio, int flags)
151 1.1 jmcneill {
152 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL)
153 1.1 jmcneill return ENXIO;
154 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_WRITE, minphys, uio);
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill static void
158 1.1 jmcneill altmemstrategy(struct buf *bp)
159 1.1 jmcneill {
160 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(bp->b_dev));
161 1.1 jmcneill
162 1.1 jmcneill if (sc == NULL) {
163 1.1 jmcneill bp->b_error = ENXIO;
164 1.1 jmcneill biodone(bp);
165 1.1 jmcneill return;
166 1.1 jmcneill }
167 1.1 jmcneill if (bp->b_bcount == 0) {
168 1.1 jmcneill biodone(bp);
169 1.1 jmcneill return;
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill sc->sc_memops->strategy(sc->sc_cookie, bp);
173 1.1 jmcneill biodone(bp);
174 1.1 jmcneill }
175 1.1 jmcneill
176 1.1 jmcneill static int
177 1.1 jmcneill altmemioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
178 1.1 jmcneill {
179 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
180 1.1 jmcneill struct dkwedge_info *dkw;
181 1.1 jmcneill
182 1.1 jmcneill switch (cmd) {
183 1.1 jmcneill case DIOCGWEDGEINFO:
184 1.1 jmcneill dkw = (void *)data;
185 1.1 jmcneill strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
186 1.1 jmcneill sizeof(dkw->dkw_devname));
187 1.1 jmcneill strlcpy(dkw->dkw_wname, "altmem", sizeof(dkw->dkw_wname));
188 1.1 jmcneill dkw->dkw_parent[0] = '\0';
189 1.1 jmcneill dkw->dkw_offset = 0;
190 1.1 jmcneill dkw->dkw_size = sc->sc_size >> DEV_BSHIFT;
191 1.1 jmcneill strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
192 1.1 jmcneill break;
193 1.1 jmcneill default:
194 1.1 jmcneill return ENOTTY;
195 1.1 jmcneill }
196 1.1 jmcneill return 0;
197 1.1 jmcneill }
198