gdrom.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: gdrom.c,v 1.1.2.2 2017/01/07 08:56:12 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2001 Marcus Comstedt
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette * 3. All advertising materials mentioning features or use of this software
16 1.1.2.2 pgoyette * must display the following acknowledgement:
17 1.1.2.2 pgoyette * This product includes software developed by Marcus Comstedt.
18 1.1.2.2 pgoyette * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.1.2.2 pgoyette * contributors may be used to endorse or promote products derived
20 1.1.2.2 pgoyette * from this software without specific prior written permission.
21 1.1.2.2 pgoyette *
22 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
33 1.1.2.2 pgoyette */
34 1.1.2.2 pgoyette
35 1.1.2.2 pgoyette /*
36 1.1.2.2 pgoyette * WIP gdrom driver using MI ATA/ATAPI drivers.
37 1.1.2.2 pgoyette *
38 1.1.2.2 pgoyette * XXX: Still not functional because GD-ROM driver does not generate
39 1.1.2.2 pgoyette * XXX: interrupts after ATAPI command packet xfers and such quirks
40 1.1.2.2 pgoyette * XXX: need to be handled in MI scsipi layer.
41 1.1.2.2 pgoyette */
42 1.1.2.2 pgoyette
43 1.1.2.2 pgoyette #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
44 1.1.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: gdrom.c,v 1.1.2.2 2017/01/07 08:56:12 pgoyette Exp $");
45 1.1.2.2 pgoyette
46 1.1.2.2 pgoyette #include <sys/param.h>
47 1.1.2.2 pgoyette #include <sys/systm.h>
48 1.1.2.2 pgoyette #include <sys/device.h>
49 1.1.2.2 pgoyette
50 1.1.2.2 pgoyette #include <sys/buf.h>
51 1.1.2.2 pgoyette #include <sys/bufq.h>
52 1.1.2.2 pgoyette #include <sys/ioctl.h>
53 1.1.2.2 pgoyette #include <sys/fcntl.h>
54 1.1.2.2 pgoyette #include <sys/disklabel.h>
55 1.1.2.2 pgoyette #include <sys/disk.h>
56 1.1.2.2 pgoyette #include <sys/cdio.h>
57 1.1.2.2 pgoyette #include <sys/proc.h>
58 1.1.2.2 pgoyette #include <sys/conf.h>
59 1.1.2.2 pgoyette #include <sys/scsiio.h>
60 1.1.2.2 pgoyette
61 1.1.2.2 pgoyette #include <dev/scsipi/scsi_spc.h>
62 1.1.2.2 pgoyette #include <dev/scsipi/scsipi_all.h>
63 1.1.2.2 pgoyette #include <dev/scsipi/scsipi_cd.h>
64 1.1.2.2 pgoyette #include <dev/scsipi/scsipi_disk.h>
65 1.1.2.2 pgoyette #include <dev/scsipi/scsi_all.h>
66 1.1.2.2 pgoyette #include <dev/scsipi/scsi_disk.h>
67 1.1.2.2 pgoyette #include <dev/scsipi/scsipiconf.h>
68 1.1.2.2 pgoyette #include <dev/scsipi/scsipi_base.h>
69 1.1.2.2 pgoyette
70 1.1.2.2 pgoyette #include "ioconf.h"
71 1.1.2.2 pgoyette
72 1.1.2.2 pgoyette struct gdrom_softc {
73 1.1.2.2 pgoyette device_t sc_dev; /* generic device info */
74 1.1.2.2 pgoyette struct disk sc_dk; /* generic disk info */
75 1.1.2.2 pgoyette struct bufq_state *sc_bufq; /* device buffer queue */
76 1.1.2.2 pgoyette struct buf curbuf; /* state of current I/O operation */
77 1.1.2.2 pgoyette
78 1.1.2.2 pgoyette kmutex_t sc_lock;
79 1.1.2.2 pgoyette struct scsipi_periph *sc_periph;
80 1.1.2.2 pgoyette
81 1.1.2.2 pgoyette bool is_open;
82 1.1.2.2 pgoyette bool is_busy;
83 1.1.2.2 pgoyette bool is_active;
84 1.1.2.2 pgoyette int openpart_start; /* start sector of currently open partition */
85 1.1.2.2 pgoyette
86 1.1.2.2 pgoyette int cmd_active;
87 1.1.2.2 pgoyette void *cmd_result_buf; /* where to store result data (16 bit aligned) */
88 1.1.2.2 pgoyette int cmd_result_size; /* number of bytes allocated for buf */
89 1.1.2.2 pgoyette int cmd_actual; /* number of bytes actually read */
90 1.1.2.2 pgoyette int cmd_cond; /* resulting condition of command */
91 1.1.2.2 pgoyette };
92 1.1.2.2 pgoyette
93 1.1.2.2 pgoyette struct gd_toc {
94 1.1.2.2 pgoyette unsigned int entry[99];
95 1.1.2.2 pgoyette unsigned int first, last;
96 1.1.2.2 pgoyette unsigned int leadout;
97 1.1.2.2 pgoyette };
98 1.1.2.2 pgoyette
99 1.1.2.2 pgoyette static int gdrommatch(device_t, cfdata_t, void *);
100 1.1.2.2 pgoyette static void gdromattach(device_t, device_t, void *);
101 1.1.2.2 pgoyette
102 1.1.2.2 pgoyette #if 0
103 1.1.2.2 pgoyette static int gdrom_command_sense(struct gdrom_softc *, void *, void *,
104 1.1.2.2 pgoyette unsigned int, int *);
105 1.1.2.2 pgoyette #endif
106 1.1.2.2 pgoyette static int gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
107 1.1.2.2 pgoyette static int gdrom_read_sectors(struct gdrom_softc *, struct buf *);
108 1.1.2.2 pgoyette static int gdrom_mount_disk(struct gdrom_softc *);
109 1.1.2.2 pgoyette static void gdrom_start(struct scsipi_periph *);
110 1.1.2.2 pgoyette static void gdrom_done(struct scsipi_xfer *, int);
111 1.1.2.2 pgoyette
112 1.1.2.2 pgoyette static const struct scsipi_inquiry_pattern gdrom_patterns[] = {
113 1.1.2.2 pgoyette {T_DIRECT, T_FIXED,
114 1.1.2.2 pgoyette "", "DCR-MOD", ""},
115 1.1.2.2 pgoyette };
116 1.1.2.2 pgoyette
117 1.1.2.2 pgoyette static dev_type_open(gdromopen);
118 1.1.2.2 pgoyette static dev_type_close(gdromclose);
119 1.1.2.2 pgoyette static dev_type_read(gdromread);
120 1.1.2.2 pgoyette static dev_type_write(gdromwrite);
121 1.1.2.2 pgoyette static dev_type_ioctl(gdromioctl);
122 1.1.2.2 pgoyette static dev_type_strategy(gdromstrategy);
123 1.1.2.2 pgoyette
124 1.1.2.2 pgoyette const struct bdevsw gdrom_bdevsw = {
125 1.1.2.2 pgoyette .d_open = gdromopen,
126 1.1.2.2 pgoyette .d_close = gdromclose,
127 1.1.2.2 pgoyette .d_strategy = gdromstrategy,
128 1.1.2.2 pgoyette .d_ioctl = gdromioctl,
129 1.1.2.2 pgoyette .d_dump = nodump,
130 1.1.2.2 pgoyette .d_psize = nosize,
131 1.1.2.2 pgoyette .d_discard = nodiscard,
132 1.1.2.2 pgoyette .d_flag = D_DISK
133 1.1.2.2 pgoyette };
134 1.1.2.2 pgoyette
135 1.1.2.2 pgoyette const struct cdevsw gdrom_cdevsw = {
136 1.1.2.2 pgoyette .d_open = gdromopen,
137 1.1.2.2 pgoyette .d_close = gdromclose,
138 1.1.2.2 pgoyette .d_read = gdromread,
139 1.1.2.2 pgoyette .d_write = gdromwrite,
140 1.1.2.2 pgoyette .d_ioctl = gdromioctl,
141 1.1.2.2 pgoyette .d_stop = nostop,
142 1.1.2.2 pgoyette .d_tty = notty,
143 1.1.2.2 pgoyette .d_poll = nopoll,
144 1.1.2.2 pgoyette .d_mmap = nommap,
145 1.1.2.2 pgoyette .d_kqfilter = nokqfilter,
146 1.1.2.2 pgoyette .d_discard = nodiscard,
147 1.1.2.2 pgoyette .d_flag = D_DISK
148 1.1.2.2 pgoyette };
149 1.1.2.2 pgoyette
150 1.1.2.2 pgoyette CFATTACH_DECL_NEW(gdrom, sizeof(struct gdrom_softc),
151 1.1.2.2 pgoyette gdrommatch, gdromattach, NULL, NULL);
152 1.1.2.2 pgoyette
153 1.1.2.2 pgoyette struct dkdriver gdromdkdriver = {
154 1.1.2.2 pgoyette .d_strategy = gdromstrategy
155 1.1.2.2 pgoyette };
156 1.1.2.2 pgoyette
157 1.1.2.2 pgoyette static const struct scsipi_periphsw gdrom_switch = {
158 1.1.2.2 pgoyette NULL/*gdrom_interpret_sense*/, /* use our error handler first */
159 1.1.2.2 pgoyette gdrom_start, /* we have a queue, which is started by this */
160 1.1.2.2 pgoyette NULL, /* we do not have an async handler */
161 1.1.2.2 pgoyette gdrom_done, /* deal with stats at interrupt time */
162 1.1.2.2 pgoyette };
163 1.1.2.2 pgoyette
164 1.1.2.2 pgoyette #define GDROMDEBUG
165 1.1.2.2 pgoyette #ifdef GDROMDEBUG
166 1.1.2.2 pgoyette int gdrom_debug = 0; /* patchable */
167 1.1.2.2 pgoyette #define GDROM_DPRINTF(x) if (gdrom_debug) printf x
168 1.1.2.2 pgoyette #else
169 1.1.2.2 pgoyette #define GDROM_DPRINTF(x) /**/
170 1.1.2.2 pgoyette #endif
171 1.1.2.2 pgoyette
172 1.1.2.2 pgoyette #define TOC_LBA(n) ((n) & 0xffffff00)
173 1.1.2.2 pgoyette #define TOC_ADR(n) ((n) & 0x0f)
174 1.1.2.2 pgoyette #define TOC_CTRL(n) (((n) & 0xf0) >> 4)
175 1.1.2.2 pgoyette #define TOC_TRACK(n) (((n) & 0x0000ff00) >> 8)
176 1.1.2.2 pgoyette
177 1.1.2.2 pgoyette #if 0
178 1.1.2.2 pgoyette int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
179 1.1.2.2 pgoyette unsigned int nbyt, int *resid)
180 1.1.2.2 pgoyette {
181 1.1.2.2 pgoyette /*
182 1.1.2.2 pgoyette * 76543210 76543210
183 1.1.2.2 pgoyette * 0 0x13 -
184 1.1.2.2 pgoyette * 2 - bufsz(hi)
185 1.1.2.2 pgoyette * 4 bufsz(lo) -
186 1.1.2.2 pgoyette * 6 - -
187 1.1.2.2 pgoyette * 8 - -
188 1.1.2.2 pgoyette * 10 - -
189 1.1.2.2 pgoyette */
190 1.1.2.2 pgoyette uint16_t sense_data[5];
191 1.1.2.2 pgoyette uint8_t cmd[12];
192 1.1.2.2 pgoyette int cond, sense_key, sense_specific;
193 1.1.2.2 pgoyette
194 1.1.2.2 pgoyette cond = scsipi_command(sc->sc_periph, req, 12, buf, nbyt,
195 1.1.2.2 pgoyette 4, 3000, NULL, XS_CTL_DATA_IN);
196 1.1.2.2 pgoyette if (resid != NULL)
197 1.1.2.2 pgoyette *resid = nbyt;
198 1.1.2.2 pgoyette
199 1.1.2.2 pgoyette if (cond < 0) {
200 1.1.2.2 pgoyette GDROM_DPRINTF(("GDROM: not ready (2:58)\n"));
201 1.1.2.2 pgoyette return EIO;
202 1.1.2.2 pgoyette }
203 1.1.2.2 pgoyette
204 1.1.2.2 pgoyette if ((cond & 1) == 0) {
205 1.1.2.2 pgoyette GDROM_DPRINTF(("GDROM: no sense. 0:0\n"));
206 1.1.2.2 pgoyette return 0;
207 1.1.2.2 pgoyette }
208 1.1.2.2 pgoyette
209 1.1.2.2 pgoyette memset(cmd, 0, sizeof(cmd));
210 1.1.2.2 pgoyette
211 1.1.2.2 pgoyette cmd[0] = 0x13;
212 1.1.2.2 pgoyette cmd[4] = sizeof(sense_data);
213 1.1.2.2 pgoyette
214 1.1.2.2 pgoyette scsipi_command(sc->sc_periph, (void *)cmd, sizeof(cmd),
215 1.1.2.2 pgoyette (void *)sense_data, sizeof(sense_data),
216 1.1.2.2 pgoyette 4, 3000, NULL, XS_CTL_DATA_IN);
217 1.1.2.2 pgoyette
218 1.1.2.2 pgoyette sense_key = sense_data[1] & 0xf;
219 1.1.2.2 pgoyette sense_specific = sense_data[4];
220 1.1.2.2 pgoyette if (sense_key == 11 && sense_specific == 0) {
221 1.1.2.2 pgoyette GDROM_DPRINTF(("GDROM: aborted (ignored). 0:0\n"));
222 1.1.2.2 pgoyette return 0;
223 1.1.2.2 pgoyette }
224 1.1.2.2 pgoyette
225 1.1.2.2 pgoyette GDROM_DPRINTF(("GDROM: SENSE %d:", sense_key));
226 1.1.2.2 pgoyette GDROM_DPRINTF(("GDROM: %d\n", sense_specific));
227 1.1.2.2 pgoyette
228 1.1.2.2 pgoyette return sense_key == 0 ? 0 : EIO;
229 1.1.2.2 pgoyette }
230 1.1.2.2 pgoyette #endif
231 1.1.2.2 pgoyette
232 1.1.2.2 pgoyette int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
233 1.1.2.2 pgoyette {
234 1.1.2.2 pgoyette /*
235 1.1.2.2 pgoyette * 76543210 76543210
236 1.1.2.2 pgoyette * 0 0x14 -
237 1.1.2.2 pgoyette * 2 - bufsz(hi)
238 1.1.2.2 pgoyette * 4 bufsz(lo) -
239 1.1.2.2 pgoyette * 6 - -
240 1.1.2.2 pgoyette * 8 - -
241 1.1.2.2 pgoyette * 10 - -
242 1.1.2.2 pgoyette */
243 1.1.2.2 pgoyette uint8_t cmd[12];
244 1.1.2.2 pgoyette
245 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
246 1.1.2.2 pgoyette memset(cmd, 0, sizeof(cmd));
247 1.1.2.2 pgoyette
248 1.1.2.2 pgoyette cmd[0] = 0x14;
249 1.1.2.2 pgoyette cmd[3] = sizeof(struct gd_toc) >> 8;
250 1.1.2.2 pgoyette cmd[4] = sizeof(struct gd_toc) & 0xff;
251 1.1.2.2 pgoyette
252 1.1.2.2 pgoyette return scsipi_command(sc->sc_periph, (void *)cmd, 12,
253 1.1.2.2 pgoyette (void *)toc, sizeof(struct gd_toc),
254 1.1.2.2 pgoyette 4, 3000, NULL, XS_CTL_DATA_IN);
255 1.1.2.2 pgoyette }
256 1.1.2.2 pgoyette
257 1.1.2.2 pgoyette int gdrom_read_sectors(struct gdrom_softc *sc, struct buf *bp)
258 1.1.2.2 pgoyette {
259 1.1.2.2 pgoyette /*
260 1.1.2.2 pgoyette * 76543210 76543210
261 1.1.2.2 pgoyette * 0 0x30 datafmt
262 1.1.2.2 pgoyette * 2 sec(hi) sec(mid)
263 1.1.2.2 pgoyette * 4 sec(lo) -
264 1.1.2.2 pgoyette * 6 - -
265 1.1.2.2 pgoyette * 8 cnt(hi) cnt(mid)
266 1.1.2.2 pgoyette * 10 cnt(lo) -
267 1.1.2.2 pgoyette */
268 1.1.2.2 pgoyette uint8_t cmd[12];
269 1.1.2.2 pgoyette void *buf;
270 1.1.2.2 pgoyette int sector, cnt;
271 1.1.2.2 pgoyette int cond;
272 1.1.2.2 pgoyette
273 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
274 1.1.2.2 pgoyette
275 1.1.2.2 pgoyette buf = bp->b_data;
276 1.1.2.2 pgoyette sector = bp->b_rawblkno;
277 1.1.2.2 pgoyette cnt = bp->b_bcount >> 11;
278 1.1.2.2 pgoyette
279 1.1.2.2 pgoyette memset(cmd, 0, sizeof(cmd));
280 1.1.2.2 pgoyette
281 1.1.2.2 pgoyette cmd[0] = 0x30;
282 1.1.2.2 pgoyette cmd[1] = 0x20;
283 1.1.2.2 pgoyette cmd[2] = sector >> 16;
284 1.1.2.2 pgoyette cmd[3] = sector >> 8;
285 1.1.2.2 pgoyette cmd[4] = sector;
286 1.1.2.2 pgoyette cmd[8] = cnt >> 16;
287 1.1.2.2 pgoyette cmd[9] = cnt >> 8;
288 1.1.2.2 pgoyette cmd[10] = cnt;
289 1.1.2.2 pgoyette
290 1.1.2.2 pgoyette cond = scsipi_command(sc->sc_periph, (void *)cmd, 12,
291 1.1.2.2 pgoyette (void *)buf, bp->b_bcount,
292 1.1.2.2 pgoyette 4, 3000, bp, XS_CTL_DATA_IN);
293 1.1.2.2 pgoyette
294 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: cond = %d\n", __func__, cond));
295 1.1.2.2 pgoyette
296 1.1.2.2 pgoyette return cond;
297 1.1.2.2 pgoyette }
298 1.1.2.2 pgoyette
299 1.1.2.2 pgoyette int gdrom_mount_disk(struct gdrom_softc *sc)
300 1.1.2.2 pgoyette {
301 1.1.2.2 pgoyette /*
302 1.1.2.2 pgoyette * 76543210 76543210
303 1.1.2.2 pgoyette * 0 0x70 -
304 1.1.2.2 pgoyette * 2 0x1f -
305 1.1.2.2 pgoyette * 4 - -
306 1.1.2.2 pgoyette * 6 - -
307 1.1.2.2 pgoyette * 8 - -
308 1.1.2.2 pgoyette * 10 - -
309 1.1.2.2 pgoyette */
310 1.1.2.2 pgoyette uint8_t cmd[12];
311 1.1.2.2 pgoyette int cond;
312 1.1.2.2 pgoyette
313 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
314 1.1.2.2 pgoyette memset(cmd, 0, sizeof(cmd));
315 1.1.2.2 pgoyette
316 1.1.2.2 pgoyette cmd[0] = 0x70;
317 1.1.2.2 pgoyette cmd[2] = 0x1f;
318 1.1.2.2 pgoyette
319 1.1.2.2 pgoyette cond = scsipi_command(sc->sc_periph, (void *)cmd, 12, NULL, 0,
320 1.1.2.2 pgoyette 4, 3000, NULL, 0);
321 1.1.2.2 pgoyette
322 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: cond = %d\n", __func__, cond));
323 1.1.2.2 pgoyette return cond;
324 1.1.2.2 pgoyette }
325 1.1.2.2 pgoyette
326 1.1.2.2 pgoyette int
327 1.1.2.2 pgoyette gdrommatch(device_t parent, cfdata_t cf, void *aux)
328 1.1.2.2 pgoyette {
329 1.1.2.2 pgoyette struct scsipibus_attach_args *sa = aux;
330 1.1.2.2 pgoyette int priority;
331 1.1.2.2 pgoyette
332 1.1.2.2 pgoyette (void)scsipi_inqmatch(&sa->sa_inqbuf,
333 1.1.2.2 pgoyette gdrom_patterns, __arraycount(gdrom_patterns),
334 1.1.2.2 pgoyette sizeof(gdrom_patterns[0]), &priority);
335 1.1.2.2 pgoyette
336 1.1.2.2 pgoyette if (priority > 0) {
337 1.1.2.2 pgoyette /* beat generic direct fixed device */
338 1.1.2.2 pgoyette priority = 255;
339 1.1.2.2 pgoyette }
340 1.1.2.2 pgoyette
341 1.1.2.2 pgoyette return priority;
342 1.1.2.2 pgoyette }
343 1.1.2.2 pgoyette
344 1.1.2.2 pgoyette void
345 1.1.2.2 pgoyette gdromattach(device_t parent, device_t self, void *aux)
346 1.1.2.2 pgoyette {
347 1.1.2.2 pgoyette struct gdrom_softc *sc;
348 1.1.2.2 pgoyette struct scsipibus_attach_args *sa;
349 1.1.2.2 pgoyette struct scsipi_periph *periph;
350 1.1.2.2 pgoyette
351 1.1.2.2 pgoyette sc = device_private(self);
352 1.1.2.2 pgoyette sa = aux;
353 1.1.2.2 pgoyette periph = sa->sa_periph;
354 1.1.2.2 pgoyette sc->sc_dev = self;
355 1.1.2.2 pgoyette sc->sc_periph = periph;
356 1.1.2.2 pgoyette periph->periph_dev = sc->sc_dev;
357 1.1.2.2 pgoyette periph->periph_switch = &gdrom_switch;
358 1.1.2.2 pgoyette
359 1.1.2.2 pgoyette mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
360 1.1.2.2 pgoyette
361 1.1.2.2 pgoyette bufq_alloc(&sc->sc_bufq, "disksort", BUFQ_SORT_RAWBLOCK);
362 1.1.2.2 pgoyette
363 1.1.2.2 pgoyette /*
364 1.1.2.2 pgoyette * Initialize and attach the disk structure.
365 1.1.2.2 pgoyette */
366 1.1.2.2 pgoyette disk_init(&sc->sc_dk, device_xname(self), &gdromdkdriver);
367 1.1.2.2 pgoyette disk_attach(&sc->sc_dk);
368 1.1.2.2 pgoyette
369 1.1.2.2 pgoyette aprint_normal("\n");
370 1.1.2.2 pgoyette aprint_naive("\n");
371 1.1.2.2 pgoyette }
372 1.1.2.2 pgoyette
373 1.1.2.2 pgoyette int
374 1.1.2.2 pgoyette gdromopen(dev_t dev, int flags, int devtype, struct lwp *l)
375 1.1.2.2 pgoyette {
376 1.1.2.2 pgoyette struct gdrom_softc *sc;
377 1.1.2.2 pgoyette int s, error, unit, cnt;
378 1.1.2.2 pgoyette struct gd_toc toc;
379 1.1.2.2 pgoyette
380 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
381 1.1.2.2 pgoyette
382 1.1.2.2 pgoyette unit = DISKUNIT(dev);
383 1.1.2.2 pgoyette
384 1.1.2.2 pgoyette sc = device_lookup_private(&gdrom_cd, unit);
385 1.1.2.2 pgoyette if (sc == NULL)
386 1.1.2.2 pgoyette return ENXIO;
387 1.1.2.2 pgoyette
388 1.1.2.2 pgoyette if (sc->is_open)
389 1.1.2.2 pgoyette return EBUSY;
390 1.1.2.2 pgoyette
391 1.1.2.2 pgoyette s = splbio();
392 1.1.2.2 pgoyette while (sc->is_busy)
393 1.1.2.2 pgoyette tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
394 1.1.2.2 pgoyette sc->is_busy = true;
395 1.1.2.2 pgoyette splx(s);
396 1.1.2.2 pgoyette
397 1.1.2.2 pgoyette for (cnt = 0; cnt < 1; cnt++)
398 1.1.2.2 pgoyette if ((error = gdrom_mount_disk(sc)) == 0)
399 1.1.2.2 pgoyette break;
400 1.1.2.2 pgoyette
401 1.1.2.2 pgoyette if (error == 0)
402 1.1.2.2 pgoyette error = gdrom_read_toc(sc, &toc);
403 1.1.2.2 pgoyette
404 1.1.2.2 pgoyette sc->is_busy = false;
405 1.1.2.2 pgoyette wakeup(&sc->is_busy);
406 1.1.2.2 pgoyette
407 1.1.2.2 pgoyette if (error != 0)
408 1.1.2.2 pgoyette return error;
409 1.1.2.2 pgoyette
410 1.1.2.2 pgoyette sc->is_open = true;
411 1.1.2.2 pgoyette sc->openpart_start = 150;
412 1.1.2.2 pgoyette
413 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: open OK\n", __func__));
414 1.1.2.2 pgoyette return 0;
415 1.1.2.2 pgoyette }
416 1.1.2.2 pgoyette
417 1.1.2.2 pgoyette int
418 1.1.2.2 pgoyette gdromclose(dev_t dev, int flags, int devtype, struct lwp *l)
419 1.1.2.2 pgoyette {
420 1.1.2.2 pgoyette struct gdrom_softc *sc;
421 1.1.2.2 pgoyette int unit;
422 1.1.2.2 pgoyette
423 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
424 1.1.2.2 pgoyette
425 1.1.2.2 pgoyette unit = DISKUNIT(dev);
426 1.1.2.2 pgoyette sc = device_lookup_private(&gdrom_cd, unit);
427 1.1.2.2 pgoyette
428 1.1.2.2 pgoyette sc->is_open = false;
429 1.1.2.2 pgoyette
430 1.1.2.2 pgoyette return 0;
431 1.1.2.2 pgoyette }
432 1.1.2.2 pgoyette
433 1.1.2.2 pgoyette void
434 1.1.2.2 pgoyette gdromstrategy(struct buf *bp)
435 1.1.2.2 pgoyette {
436 1.1.2.2 pgoyette struct gdrom_softc *sc;
437 1.1.2.2 pgoyette struct scsipi_periph *periph;
438 1.1.2.2 pgoyette int s, unit;
439 1.1.2.2 pgoyette
440 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
441 1.1.2.2 pgoyette
442 1.1.2.2 pgoyette unit = DISKUNIT(bp->b_dev);
443 1.1.2.2 pgoyette sc = device_lookup_private(&gdrom_cd, unit);
444 1.1.2.2 pgoyette periph = sc->sc_periph;
445 1.1.2.2 pgoyette
446 1.1.2.2 pgoyette if (bp->b_bcount == 0)
447 1.1.2.2 pgoyette goto done;
448 1.1.2.2 pgoyette
449 1.1.2.2 pgoyette bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
450 1.1.2.2 pgoyette
451 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: read_sectors(%p, %lld, %d) [%d bytes]\n", __func__,
452 1.1.2.2 pgoyette bp->b_data, bp->b_rawblkno,
453 1.1.2.2 pgoyette bp->b_bcount >> 11, bp->b_bcount));
454 1.1.2.2 pgoyette
455 1.1.2.2 pgoyette s = splbio();
456 1.1.2.2 pgoyette bufq_put(sc->sc_bufq, bp);
457 1.1.2.2 pgoyette splx(s);
458 1.1.2.2 pgoyette if (!sc->is_active)
459 1.1.2.2 pgoyette gdrom_start(periph);
460 1.1.2.2 pgoyette return;
461 1.1.2.2 pgoyette
462 1.1.2.2 pgoyette done:
463 1.1.2.2 pgoyette bp->b_resid = bp->b_bcount;
464 1.1.2.2 pgoyette biodone(bp);
465 1.1.2.2 pgoyette }
466 1.1.2.2 pgoyette
467 1.1.2.2 pgoyette void
468 1.1.2.2 pgoyette gdrom_start(struct scsipi_periph *periph)
469 1.1.2.2 pgoyette {
470 1.1.2.2 pgoyette struct gdrom_softc *sc = device_private(periph->periph_dev);
471 1.1.2.2 pgoyette struct buf *bp;
472 1.1.2.2 pgoyette int error, s;
473 1.1.2.2 pgoyette
474 1.1.2.2 pgoyette sc->is_active = true;
475 1.1.2.2 pgoyette
476 1.1.2.2 pgoyette for (;;) {
477 1.1.2.2 pgoyette s = splbio();
478 1.1.2.2 pgoyette bp = bufq_get(sc->sc_bufq);
479 1.1.2.2 pgoyette if (bp == NULL) {
480 1.1.2.2 pgoyette splx(s);
481 1.1.2.2 pgoyette break;
482 1.1.2.2 pgoyette }
483 1.1.2.2 pgoyette
484 1.1.2.2 pgoyette while (sc->is_busy)
485 1.1.2.2 pgoyette tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
486 1.1.2.2 pgoyette sc->is_busy = true;
487 1.1.2.2 pgoyette disk_busy(&sc->sc_dk);
488 1.1.2.2 pgoyette splx(s);
489 1.1.2.2 pgoyette
490 1.1.2.2 pgoyette error = gdrom_read_sectors(sc, bp);
491 1.1.2.2 pgoyette bp->b_error = error;
492 1.1.2.2 pgoyette if (error != 0)
493 1.1.2.2 pgoyette bp->b_resid = bp->b_bcount;
494 1.1.2.2 pgoyette
495 1.1.2.2 pgoyette sc->is_busy = false;
496 1.1.2.2 pgoyette wakeup(&sc->is_busy);
497 1.1.2.2 pgoyette
498 1.1.2.2 pgoyette s = splbio();
499 1.1.2.2 pgoyette disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
500 1.1.2.2 pgoyette (bp->b_flags & B_READ) != 0);
501 1.1.2.2 pgoyette splx(s);
502 1.1.2.2 pgoyette biodone(bp);
503 1.1.2.2 pgoyette }
504 1.1.2.2 pgoyette
505 1.1.2.2 pgoyette sc->is_active = false;
506 1.1.2.2 pgoyette }
507 1.1.2.2 pgoyette
508 1.1.2.2 pgoyette void
509 1.1.2.2 pgoyette gdrom_done(struct scsipi_xfer *xs, int error)
510 1.1.2.2 pgoyette {
511 1.1.2.2 pgoyette
512 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
513 1.1.2.2 pgoyette }
514 1.1.2.2 pgoyette
515 1.1.2.2 pgoyette int
516 1.1.2.2 pgoyette gdromioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
517 1.1.2.2 pgoyette {
518 1.1.2.2 pgoyette struct gdrom_softc *sc;
519 1.1.2.2 pgoyette int unit, error;
520 1.1.2.2 pgoyette
521 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: cmd %lx\n", __func__, cmd));
522 1.1.2.2 pgoyette
523 1.1.2.2 pgoyette unit = DISKUNIT(dev);
524 1.1.2.2 pgoyette sc = device_lookup_private(&gdrom_cd, unit);
525 1.1.2.2 pgoyette
526 1.1.2.2 pgoyette switch (cmd) {
527 1.1.2.2 pgoyette case CDIOREADMSADDR: {
528 1.1.2.2 pgoyette int s, track, sessno = *(int *)addr;
529 1.1.2.2 pgoyette struct gd_toc toc;
530 1.1.2.2 pgoyette
531 1.1.2.2 pgoyette if (sessno != 0)
532 1.1.2.2 pgoyette return EINVAL;
533 1.1.2.2 pgoyette
534 1.1.2.2 pgoyette s = splbio();
535 1.1.2.2 pgoyette while (sc->is_busy)
536 1.1.2.2 pgoyette tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
537 1.1.2.2 pgoyette sc->is_busy = true;
538 1.1.2.2 pgoyette splx(s);
539 1.1.2.2 pgoyette
540 1.1.2.2 pgoyette error = gdrom_read_toc(sc, &toc);
541 1.1.2.2 pgoyette
542 1.1.2.2 pgoyette sc->is_busy = false;
543 1.1.2.2 pgoyette wakeup(&sc->is_busy);
544 1.1.2.2 pgoyette
545 1.1.2.2 pgoyette if (error != 0)
546 1.1.2.2 pgoyette return error;
547 1.1.2.2 pgoyette #ifdef GDROMDEBUGTOC
548 1.1.2.2 pgoyette { /* Dump the GDROM TOC */
549 1.1.2.2 pgoyette unsigned char *ptr = (unsigned char *)&toc;
550 1.1.2.2 pgoyette int i;
551 1.1.2.2 pgoyette
552 1.1.2.2 pgoyette printf("gdrom: TOC\n");
553 1.1.2.2 pgoyette for(i = 0; i < sizeof(toc); ++i) {
554 1.1.2.2 pgoyette printf("%02x", *ptr++);
555 1.1.2.2 pgoyette if( i%32 == 31)
556 1.1.2.2 pgoyette printf("\n");
557 1.1.2.2 pgoyette else if( i%4 == 3)
558 1.1.2.2 pgoyette printf(",");
559 1.1.2.2 pgoyette }
560 1.1.2.2 pgoyette printf("\n");
561 1.1.2.2 pgoyette }
562 1.1.2.2 pgoyette #endif
563 1.1.2.2 pgoyette for (track = TOC_TRACK(toc.last);
564 1.1.2.2 pgoyette track >= TOC_TRACK(toc.first);
565 1.1.2.2 pgoyette --track) {
566 1.1.2.2 pgoyette if (track < 1 || track > 100)
567 1.1.2.2 pgoyette return ENXIO;
568 1.1.2.2 pgoyette if (TOC_CTRL(toc.entry[track - 1]))
569 1.1.2.2 pgoyette break;
570 1.1.2.2 pgoyette }
571 1.1.2.2 pgoyette
572 1.1.2.2 pgoyette #ifdef GDROMDEBUGTOC
573 1.1.2.2 pgoyette printf("gdrom: Using track %d, LBA %u\n", track,
574 1.1.2.2 pgoyette TOC_LBA(toc.entry[track - 1]));
575 1.1.2.2 pgoyette #endif
576 1.1.2.2 pgoyette
577 1.1.2.2 pgoyette *(int *)addr = htonl(TOC_LBA(toc.entry[track - 1])) -
578 1.1.2.2 pgoyette sc->openpart_start;
579 1.1.2.2 pgoyette
580 1.1.2.2 pgoyette return 0;
581 1.1.2.2 pgoyette }
582 1.1.2.2 pgoyette default:
583 1.1.2.2 pgoyette return ENOTTY;
584 1.1.2.2 pgoyette }
585 1.1.2.2 pgoyette
586 1.1.2.2 pgoyette #ifdef DIAGNOSTIC
587 1.1.2.2 pgoyette panic("gdromioctl: impossible");
588 1.1.2.2 pgoyette #endif
589 1.1.2.2 pgoyette }
590 1.1.2.2 pgoyette
591 1.1.2.2 pgoyette
592 1.1.2.2 pgoyette int
593 1.1.2.2 pgoyette gdromread(dev_t dev, struct uio *uio, int flags)
594 1.1.2.2 pgoyette {
595 1.1.2.2 pgoyette
596 1.1.2.2 pgoyette GDROM_DPRINTF(("%s: called\n", __func__));
597 1.1.2.2 pgoyette return physio(gdromstrategy, NULL, dev, B_READ, minphys, uio);
598 1.1.2.2 pgoyette }
599 1.1.2.2 pgoyette
600 1.1.2.2 pgoyette int
601 1.1.2.2 pgoyette gdromwrite(dev_t dev, struct uio *uio, int flags)
602 1.1.2.2 pgoyette {
603 1.1.2.2 pgoyette
604 1.1.2.2 pgoyette return EROFS;
605 1.1.2.2 pgoyette }
606