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