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