Home | History | Annotate | Line # | Download | only in dev
gdrom.c revision 1.38
      1  1.38    martin /*	$NetBSD: gdrom.c,v 1.38 2014/03/18 08:08:55 martin 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.38    martin __KERNEL_RCSID(0, "$NetBSD: gdrom.c,v 1.38 2014/03/18 08:08:55 martin 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.37  dholland 	.d_open = gdromopen,
     68  1.37  dholland 	.d_close = gdromclose,
     69  1.38    martin 	.d_strategy = gdromstrategy,
     70  1.37  dholland 	.d_ioctl = gdromioctl,
     71  1.37  dholland 	.d_dump = nodump,
     72  1.37  dholland 	.d_psize = nosize,
     73  1.37  dholland 	.d_flag = D_DISK
     74  1.10   gehenna };
     75  1.10   gehenna 
     76  1.10   gehenna const struct cdevsw gdrom_cdevsw = {
     77  1.37  dholland 	.d_open = gdromopen,
     78  1.37  dholland 	.d_close = gdromclose,
     79  1.37  dholland 	.d_read = gdromread,
     80  1.37  dholland 	.d_write = gdromwrite,
     81  1.37  dholland 	.d_ioctl = gdromioctl,
     82  1.37  dholland 	.d_stop = nostop,
     83  1.37  dholland 	.d_tty = notty,
     84  1.37  dholland 	.d_poll = nopoll,
     85  1.37  dholland 	.d_mmap = nommap,
     86  1.37  dholland 	.d_kqfilter = nokqfilter,
     87  1.37  dholland 	.d_flag = D_DISK
     88  1.10   gehenna };
     89   1.1    marcus 
     90   1.1    marcus struct gdrom_softc {
     91  1.28   tsutsui 	device_t sc_dev;	/* generic device info */
     92  1.30   tsutsui 	struct disk sc_dk;	/* generic disk info */
     93  1.30   tsutsui 	struct bufq_state *sc_bufq;	/* device buffer queue */
     94   1.1    marcus 	struct buf curbuf;	/* state of current I/O operation */
     95   1.1    marcus 
     96  1.32   tsutsui 	bool is_open;
     97  1.32   tsutsui 	bool is_busy;
     98  1.30   tsutsui 	bool is_active;
     99   1.1    marcus 	int openpart_start;	/* start sector of currently open partition */
    100   1.3    marcus 
    101   1.3    marcus 	int cmd_active;
    102   1.3    marcus 	void *cmd_result_buf;	/* where to store result data (16 bit aligned) */
    103   1.3    marcus 	int cmd_result_size;	/* number of bytes allocated for buf */
    104   1.3    marcus 	int cmd_actual;		/* number of bytes actually read */
    105   1.3    marcus 	int cmd_cond;		/* resulting condition of command */
    106   1.1    marcus };
    107   1.1    marcus 
    108  1.28   tsutsui CFATTACH_DECL_NEW(gdrom, sizeof(struct gdrom_softc),
    109  1.13   thorpej     gdrommatch, gdromattach, NULL, NULL);
    110   1.1    marcus 
    111   1.1    marcus struct dkdriver gdromdkdriver = { gdromstrategy };
    112   1.1    marcus 
    113   1.1    marcus 
    114   1.1    marcus struct gd_toc {
    115   1.8       uch 	unsigned int entry[99];
    116   1.8       uch 	unsigned int first, last;
    117   1.8       uch 	unsigned int leadout;
    118   1.1    marcus };
    119   1.1    marcus 
    120  1.34   tsutsui #ifdef GDROMDEBUG
    121  1.34   tsutsui #define DPRINTF(x)	printf x
    122  1.34   tsutsui #else
    123  1.34   tsutsui #define DPRINTF(x)	/**/
    124  1.34   tsutsui #endif
    125  1.34   tsutsui 
    126   1.8       uch #define TOC_LBA(n)	((n) & 0xffffff00)
    127   1.8       uch #define TOC_ADR(n)	((n) & 0x0f)
    128   1.8       uch #define TOC_CTRL(n)	(((n) & 0xf0) >> 4)
    129   1.8       uch #define TOC_TRACK(n)	(((n) & 0x0000ff00) >> 8)
    130   1.8       uch 
    131  1.32   tsutsui #define GDROM(o)	(*(volatile uint8_t *)(0xa05f7000 + (o)))
    132   1.8       uch 
    133   1.8       uch #define GDSTATSTAT(n)	((n) & 0xf)
    134   1.8       uch #define GDSTATDISK(n)	(((n) >> 4) & 0xf)
    135   1.8       uch 
    136   1.8       uch #define GDROM_BUSY	GDROM(0x18)
    137  1.32   tsutsui #define GDROM_DATA	(*(volatile uint16_t *)(&GDROM(0x80)))
    138   1.8       uch #define GDROM_REGX	GDROM(0x84)
    139   1.8       uch #define GDROM_STAT	GDROM(0x8c)
    140   1.8       uch #define GDROM_CNTLO	GDROM(0x90)
    141   1.8       uch #define GDROM_CNTHI	GDROM(0x94)
    142   1.8       uch #define GDROM_COND	GDROM(0x9c)
    143   1.8       uch 
    144  1.33   tsutsui #if 0
    145  1.33   tsutsui static int gdrom_getstat(void);
    146  1.33   tsutsui #endif
    147  1.33   tsutsui static int gdrom_do_command(struct gdrom_softc *, void *, void *,
    148  1.33   tsutsui     unsigned int, int *);
    149  1.33   tsutsui static int gdrom_command_sense(struct gdrom_softc *, void *, void *,
    150  1.33   tsutsui     unsigned int, int *);
    151  1.33   tsutsui static int gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
    152  1.33   tsutsui static int gdrom_read_sectors(struct gdrom_softc *, void *, int, int,
    153  1.33   tsutsui     int *);
    154  1.33   tsutsui static int gdrom_mount_disk(struct gdrom_softc *);
    155  1.33   tsutsui static int gdrom_intr(void *);
    156  1.33   tsutsui static void gdrom_start(struct gdrom_softc *);
    157   1.1    marcus 
    158  1.33   tsutsui #if 0
    159  1.33   tsutsui int
    160  1.33   tsutsui gdrom_getstat(void)
    161   1.1    marcus {
    162  1.32   tsutsui 	uint8_t s1, s2, s3;
    163   1.1    marcus 
    164   1.8       uch 	if (GDROM_BUSY & 0x80)
    165  1.17   tsutsui 		return -1;
    166   1.1    marcus 	s1 = GDROM_STAT;
    167   1.1    marcus 	s2 = GDROM_STAT;
    168   1.1    marcus 	s3 = GDROM_STAT;
    169  1.17   tsutsui 	if (GDROM_BUSY & 0x80)
    170  1.17   tsutsui 		return -1;
    171  1.17   tsutsui 	if (s1 == s2)
    172  1.17   tsutsui 		return s1;
    173  1.17   tsutsui 	else if (s2 == s3)
    174  1.17   tsutsui 		return s2;
    175   1.1    marcus 	else
    176  1.17   tsutsui 		return -1;
    177   1.1    marcus }
    178  1.33   tsutsui #endif
    179   1.1    marcus 
    180   1.3    marcus int
    181   1.8       uch gdrom_intr(void *arg)
    182   1.1    marcus {
    183   1.3    marcus 	struct gdrom_softc *sc = arg;
    184  1.32   tsutsui 	int s;
    185  1.32   tsutsui 	uint8_t cond;
    186   1.3    marcus 
    187   1.3    marcus 	s = splbio();
    188   1.3    marcus 	cond = GDROM_COND;
    189  1.34   tsutsui 	DPRINTF(("GDROM: cond = %x\n", cond));
    190  1.17   tsutsui 	if (!sc->cmd_active) {
    191  1.34   tsutsui 		DPRINTF(("GDROM: inactive IRQ!?\n"));
    192   1.8       uch 		splx(s);
    193  1.17   tsutsui 		return 0;
    194   1.3    marcus 	}
    195   1.3    marcus 
    196  1.34   tsutsui 	if ((cond & 0x08) != 0) {
    197   1.8       uch 		int cnt = (GDROM_CNTHI << 8) | GDROM_CNTLO;
    198  1.34   tsutsui 		DPRINTF(("GDROM: cnt = %d\n", cnt));
    199   1.3    marcus 		sc->cmd_actual += cnt;
    200  1.17   tsutsui 		if (cnt > 0 && sc->cmd_result_size > 0) {
    201   1.8       uch 			int subcnt = (cnt > sc->cmd_result_size ?
    202   1.8       uch 			    sc->cmd_result_size : cnt);
    203  1.32   tsutsui 			uint16_t *ptr = sc->cmd_result_buf;
    204  1.32   tsutsui 			sc->cmd_result_buf = ((uint8_t *)sc->cmd_result_buf) +
    205   1.8       uch 			    subcnt;
    206   1.3    marcus 			sc->cmd_result_size -= subcnt;
    207   1.3    marcus 			cnt -= subcnt;
    208   1.8       uch 			while (subcnt > 0) {
    209   1.8       uch 				*ptr++ = GDROM_DATA;
    210   1.8       uch 				subcnt -= 2;
    211   1.3    marcus 			}
    212   1.3    marcus 		}
    213   1.8       uch 		while (cnt > 0) {
    214  1.32   tsutsui 			(void)GDROM_DATA;
    215   1.8       uch 			cnt -= 2;
    216   1.3    marcus 		}
    217   1.3    marcus 	}
    218  1.34   tsutsui 	while ((GDROM_BUSY & 0x80) != 0);
    219   1.3    marcus 
    220  1.34   tsutsui 	if ((cond & 0x08) == 0) {
    221   1.3    marcus 		sc->cmd_cond = cond;
    222   1.3    marcus 		sc->cmd_active = 0;
    223   1.3    marcus 		wakeup(&sc->cmd_active);
    224   1.3    marcus 	}
    225   1.1    marcus 
    226   1.3    marcus 	splx(s);
    227  1.17   tsutsui 	return 1;
    228   1.1    marcus }
    229   1.1    marcus 
    230   1.3    marcus 
    231  1.34   tsutsui int
    232  1.34   tsutsui gdrom_do_command(struct gdrom_softc *sc, void *req, void *buf,
    233  1.27   tsutsui     unsigned int nbyt, int *resid)
    234   1.1    marcus {
    235   1.3    marcus 	int i, s;
    236  1.32   tsutsui 	uint16_t *ptr = req;
    237   1.3    marcus 
    238   1.8       uch 	while (GDROM_BUSY & 0x88)
    239   1.8       uch 		;
    240   1.8       uch 	if (buf != NULL) {
    241   1.8       uch 		GDROM_CNTLO = nbyt & 0xff;
    242   1.8       uch 		GDROM_CNTHI = (nbyt >> 8) & 0xff;
    243   1.8       uch 		GDROM_REGX = 0;
    244   1.3    marcus 	}
    245   1.3    marcus 	sc->cmd_result_buf = buf;
    246   1.3    marcus 	sc->cmd_result_size = nbyt;
    247   1.1    marcus 
    248  1.34   tsutsui 	if (GDSTATSTAT(GDROM_STAT) == 0x06)
    249  1.17   tsutsui 		return -1;
    250   1.1    marcus 
    251   1.1    marcus 	GDROM_COND = 0xa0;
    252  1.31   tsutsui 	DELAY(1);
    253  1.34   tsutsui 	while ((GDROM_BUSY & 0x88) != 0x08)
    254   1.8       uch 		;
    255   1.3    marcus 
    256   1.3    marcus 	s = splbio();
    257   1.3    marcus 
    258   1.3    marcus 	sc->cmd_actual = 0;
    259   1.3    marcus 	sc->cmd_active = 1;
    260   1.3    marcus 
    261  1.34   tsutsui 	for (i = 0; i < 6; i++)
    262   1.8       uch 		GDROM_DATA = ptr[i];
    263   1.1    marcus 
    264   1.8       uch 	while (sc->cmd_active)
    265   1.8       uch 		tsleep(&sc->cmd_active, PRIBIO, "gdrom", 0);
    266   1.3    marcus 
    267   1.3    marcus 	splx(s);
    268   1.3    marcus 
    269  1.27   tsutsui 	if (resid != NULL)
    270  1.27   tsutsui 		*resid = sc->cmd_result_size;
    271  1.27   tsutsui 
    272  1.17   tsutsui 	return sc->cmd_cond;
    273   1.1    marcus }
    274   1.1    marcus 
    275   1.3    marcus 
    276   1.8       uch int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
    277  1.27   tsutsui     unsigned int nbyt, int *resid)
    278   1.1    marcus {
    279  1.34   tsutsui 	/*
    280  1.34   tsutsui 	 *  76543210 76543210
    281  1.34   tsutsui 	 *  0   0x13      -
    282  1.34   tsutsui 	 *  2    -      bufsz(hi)
    283  1.34   tsutsui 	 *  4 bufsz(lo)   -
    284  1.34   tsutsui 	 *  6    -        -
    285  1.34   tsutsui 	 *  8    -        -
    286  1.34   tsutsui 	 * 10    -        -
    287  1.34   tsutsui 	 */
    288  1.32   tsutsui 	uint16_t sense_data[5];
    289  1.32   tsutsui 	uint8_t cmd[12];
    290  1.34   tsutsui 	int cond, sense_key, sense_specific;
    291   1.1    marcus 
    292  1.34   tsutsui 	cond = gdrom_do_command(sc, req, buf, nbyt, resid);
    293   1.3    marcus 
    294   1.8       uch 	if (cond < 0) {
    295  1.34   tsutsui 		DPRINTF(("GDROM: not ready (2:58)\n"));
    296  1.17   tsutsui 		return EIO;
    297   1.1    marcus 	}
    298   1.1    marcus 
    299   1.8       uch 	if ((cond & 1) == 0) {
    300  1.34   tsutsui 		DPRINTF(("GDROM: no sense.  0:0\n"));
    301  1.17   tsutsui 		return 0;
    302   1.1    marcus 	}
    303   1.1    marcus 
    304   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    305   1.1    marcus 
    306   1.1    marcus 	cmd[0] = 0x13;
    307   1.1    marcus 	cmd[4] = sizeof(sense_data);
    308   1.1    marcus 
    309  1.27   tsutsui 	gdrom_do_command(sc, cmd, sense_data, sizeof(sense_data), NULL);
    310   1.1    marcus 
    311   1.1    marcus 	sense_key = sense_data[1] & 0xf;
    312   1.1    marcus 	sense_specific = sense_data[4];
    313   1.8       uch 	if (sense_key == 11 && sense_specific == 0) {
    314  1.34   tsutsui 		DPRINTF(("GDROM: aborted (ignored).  0:0\n"));
    315  1.17   tsutsui 		return 0;
    316   1.1    marcus 	}
    317   1.1    marcus 
    318  1.34   tsutsui 	DPRINTF(("GDROM: SENSE %d:", sense_key));
    319  1.34   tsutsui 	DPRINTF(("GDROM: %d\n", sense_specific));
    320   1.1    marcus 
    321  1.17   tsutsui 	return sense_key == 0 ? 0 : EIO;
    322   1.1    marcus }
    323   1.1    marcus 
    324   1.8       uch int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
    325   1.1    marcus {
    326  1.34   tsutsui 	/*
    327  1.34   tsutsui 	 *  76543210 76543210
    328  1.34   tsutsui 	 *  0   0x14      -
    329  1.34   tsutsui 	 *  2    -      bufsz(hi)
    330  1.34   tsutsui 	 *  4 bufsz(lo)   -
    331  1.34   tsutsui 	 *  6    -        -
    332  1.34   tsutsui 	 *  8    -        -
    333  1.34   tsutsui 	 * 10    -        -
    334  1.34   tsutsui 	 */
    335  1.32   tsutsui 	uint8_t cmd[12];
    336   1.1    marcus 
    337   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    338   1.1    marcus 
    339   1.1    marcus 	cmd[0] = 0x14;
    340   1.8       uch 	cmd[3] = sizeof(struct gd_toc) >> 8;
    341   1.8       uch 	cmd[4] = sizeof(struct gd_toc) & 0xff;
    342   1.1    marcus 
    343  1.27   tsutsui 	return gdrom_command_sense(sc, cmd, toc, sizeof(struct gd_toc), NULL);
    344   1.1    marcus }
    345   1.1    marcus 
    346  1.27   tsutsui int gdrom_read_sectors(struct gdrom_softc *sc, void *buf, int sector, int cnt,
    347  1.27   tsutsui     int *resid)
    348   1.1    marcus {
    349  1.34   tsutsui 	/*
    350  1.34   tsutsui 	 *  76543210 76543210
    351  1.34   tsutsui 	 *  0   0x30    datafmt
    352  1.34   tsutsui 	 *  2  sec(hi)  sec(mid)
    353  1.34   tsutsui 	 *  4  sec(lo)    -
    354  1.34   tsutsui 	 *  6    -        -
    355  1.34   tsutsui 	 *  8  cnt(hi)  cnt(mid)
    356  1.34   tsutsui 	 * 10  cnt(lo)    -
    357  1.34   tsutsui 	 */
    358  1.32   tsutsui 	uint8_t cmd[12];
    359   1.1    marcus 
    360   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    361   1.1    marcus 
    362  1.34   tsutsui 	cmd[0]  = 0x30;
    363  1.34   tsutsui 	cmd[1]  = 0x20;
    364  1.34   tsutsui 	cmd[2]  = sector >> 16;
    365  1.34   tsutsui 	cmd[3]  = sector >>  8;
    366  1.34   tsutsui 	cmd[4]  = sector;
    367  1.34   tsutsui 	cmd[8]  = cnt >> 16;
    368  1.34   tsutsui 	cmd[9]  = cnt >>  8;
    369   1.1    marcus 	cmd[10] = cnt;
    370   1.1    marcus 
    371  1.27   tsutsui 	return gdrom_command_sense(sc, cmd, buf, cnt << 11, resid);
    372   1.1    marcus }
    373   1.1    marcus 
    374   1.8       uch int gdrom_mount_disk(struct gdrom_softc *sc)
    375   1.1    marcus {
    376  1.34   tsutsui 	/*
    377  1.34   tsutsui 	 *  76543210 76543210
    378  1.34   tsutsui 	 *  0   0x70      -
    379  1.34   tsutsui 	 *  2   0x1f      -
    380  1.34   tsutsui 	 *  4    -        -
    381  1.34   tsutsui 	 *  6    -        -
    382  1.34   tsutsui 	 *  8    -        -
    383  1.34   tsutsui 	 * 10    -        -
    384  1.34   tsutsui 	 */
    385  1.32   tsutsui 	uint8_t cmd[12];
    386   1.1    marcus 
    387   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    388   1.1    marcus 
    389   1.1    marcus 	cmd[0] = 0x70;
    390   1.1    marcus 	cmd[1] = 0x1f;
    391   1.1    marcus 
    392  1.27   tsutsui 	return gdrom_command_sense(sc, cmd, NULL, 0, NULL);
    393   1.1    marcus }
    394   1.1    marcus 
    395   1.1    marcus int
    396  1.28   tsutsui gdrommatch(device_t parent, cfdata_t cf, void *aux)
    397   1.1    marcus {
    398   1.1    marcus 	static int gdrom_matched = 0;
    399   1.1    marcus 
    400   1.1    marcus 	/* Allow only once instance. */
    401  1.11   thorpej 	if (gdrom_matched)
    402  1.17   tsutsui 		return 0;
    403   1.1    marcus 	gdrom_matched = 1;
    404   1.7       uch 
    405  1.17   tsutsui 	return 1;
    406   1.1    marcus }
    407   1.1    marcus 
    408   1.1    marcus void
    409  1.28   tsutsui gdromattach(device_t parent, device_t self, void *aux)
    410   1.1    marcus {
    411   1.1    marcus 	struct gdrom_softc *sc;
    412  1.36    martin 	uint32_t p;
    413   1.1    marcus 
    414  1.28   tsutsui 	sc = device_private(self);
    415  1.28   tsutsui 	sc->sc_dev = self;
    416   1.1    marcus 
    417  1.30   tsutsui 	bufq_alloc(&sc->sc_bufq, "disksort", BUFQ_SORT_RAWBLOCK);
    418  1.30   tsutsui 
    419   1.1    marcus 	/*
    420   1.1    marcus 	 * Initialize and attach the disk structure.
    421   1.1    marcus 	 */
    422  1.30   tsutsui 	disk_init(&sc->sc_dk, device_xname(self), &gdromdkdriver);
    423  1.30   tsutsui 	disk_attach(&sc->sc_dk);
    424   1.1    marcus 
    425   1.1    marcus 	/*
    426   1.1    marcus 	 * reenable disabled drive
    427   1.1    marcus 	 */
    428  1.19     perry 	*((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
    429   1.8       uch 	for (p = 0; p < 0x200000 / 4; p++)
    430  1.36    martin 		(void)((volatile uint32_t *)0xa0000000)[p];
    431   1.1    marcus 
    432  1.26    marcus 	printf(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
    433  1.21   tsutsui 	sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
    434  1.21   tsutsui 	    gdrom_intr, sc);
    435   1.1    marcus }
    436   1.1    marcus 
    437   1.1    marcus int
    438  1.18  christos gdromopen(dev_t dev, int flags, int devtype, struct lwp *l)
    439   1.1    marcus {
    440   1.1    marcus 	struct gdrom_softc *sc;
    441   1.3    marcus 	int s, error, unit, cnt;
    442   1.1    marcus 	struct gd_toc toc;
    443   1.1    marcus 
    444  1.34   tsutsui 	DPRINTF(("GDROM: open\n"));
    445   1.1    marcus 
    446   1.1    marcus 	unit = DISKUNIT(dev);
    447   1.1    marcus 
    448  1.25   tsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
    449   1.1    marcus 	if (sc == NULL)
    450  1.17   tsutsui 		return ENXIO;
    451   1.1    marcus 
    452   1.1    marcus 	if (sc->is_open)
    453  1.17   tsutsui 		return EBUSY;
    454   1.1    marcus 
    455   1.3    marcus 	s = splbio();
    456  1.17   tsutsui 	while (sc->is_busy)
    457   1.8       uch 		tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    458  1.32   tsutsui 	sc->is_busy = true;
    459   1.3    marcus 	splx(s);
    460   1.3    marcus 
    461   1.1    marcus 	for (cnt = 0; cnt < 5; cnt++)
    462   1.8       uch 		if ((error = gdrom_mount_disk(sc)) == 0)
    463   1.8       uch 			break;
    464   1.1    marcus 
    465  1.34   tsutsui 	if (error == 0)
    466   1.3    marcus 		error = gdrom_read_toc(sc, &toc);
    467   1.3    marcus 
    468  1.32   tsutsui 	sc->is_busy = false;
    469   1.3    marcus 	wakeup(&sc->is_busy);
    470   1.3    marcus 
    471  1.34   tsutsui 	if (error != 0)
    472   1.1    marcus 		return error;
    473   1.1    marcus 
    474  1.32   tsutsui 	sc->is_open = true;
    475   1.2    marcus 	sc->openpart_start = 150;
    476   1.1    marcus 
    477  1.34   tsutsui 	DPRINTF(("GDROM: open OK\n"));
    478  1.17   tsutsui 	return 0;
    479   1.1    marcus }
    480   1.1    marcus 
    481   1.1    marcus int
    482  1.18  christos gdromclose(dev_t dev, int flags, int devtype, struct lwp *l)
    483   1.1    marcus {
    484   1.1    marcus 	struct gdrom_softc *sc;
    485   1.1    marcus 	int unit;
    486  1.34   tsutsui 
    487  1.34   tsutsui 	DPRINTF(("GDROM: close\n"));
    488  1.34   tsutsui 
    489   1.1    marcus 	unit = DISKUNIT(dev);
    490  1.25   tsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
    491   1.1    marcus 
    492  1.32   tsutsui 	sc->is_open = false;
    493   1.1    marcus 
    494  1.17   tsutsui 	return 0;
    495   1.1    marcus }
    496   1.1    marcus 
    497   1.1    marcus void
    498   1.8       uch gdromstrategy(struct buf *bp)
    499   1.1    marcus {
    500   1.1    marcus 	struct gdrom_softc *sc;
    501  1.30   tsutsui 	int s, unit;
    502  1.34   tsutsui 
    503  1.34   tsutsui 	DPRINTF(("GDROM: strategy\n"));
    504   1.1    marcus 
    505   1.1    marcus 	unit = DISKUNIT(bp->b_dev);
    506  1.25   tsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
    507   1.1    marcus 
    508   1.1    marcus 	if (bp->b_bcount == 0)
    509   1.8       uch 		goto done;
    510   1.1    marcus 
    511   1.1    marcus 	bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
    512   1.1    marcus 
    513  1.34   tsutsui 	DPRINTF(("GDROM: read_sectors(%p, %lld, %d) [%d bytes]\n",
    514   1.8       uch 	    bp->b_data, bp->b_rawblkno,
    515  1.34   tsutsui 	    bp->b_bcount >> 11, bp->b_bcount));
    516  1.34   tsutsui 
    517   1.3    marcus 	s = splbio();
    518  1.30   tsutsui 	bufq_put(sc->sc_bufq, bp);
    519   1.3    marcus 	splx(s);
    520  1.30   tsutsui 	if (!sc->is_active)
    521  1.30   tsutsui 		gdrom_start(sc);
    522  1.27   tsutsui 	return;
    523   1.3    marcus 
    524   1.8       uch  done:
    525   1.1    marcus 	bp->b_resid = bp->b_bcount;
    526   1.1    marcus 	biodone(bp);
    527   1.1    marcus }
    528   1.1    marcus 
    529  1.30   tsutsui void
    530  1.30   tsutsui gdrom_start(struct gdrom_softc *sc)
    531  1.30   tsutsui {
    532  1.30   tsutsui 	struct buf *bp;
    533  1.30   tsutsui 	int error, resid, s;
    534  1.30   tsutsui 
    535  1.30   tsutsui 	sc->is_active = true;
    536  1.30   tsutsui 
    537  1.30   tsutsui 	for (;;) {
    538  1.30   tsutsui 		s = splbio();
    539  1.30   tsutsui 		bp = bufq_get(sc->sc_bufq);
    540  1.30   tsutsui 		if (bp == NULL) {
    541  1.30   tsutsui 			splx(s);
    542  1.30   tsutsui 			break;
    543  1.30   tsutsui 		}
    544  1.30   tsutsui 
    545  1.30   tsutsui 		while (sc->is_busy)
    546  1.30   tsutsui 			tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    547  1.32   tsutsui 		sc->is_busy = true;
    548  1.30   tsutsui 		disk_busy(&sc->sc_dk);
    549  1.30   tsutsui 		splx(s);
    550  1.30   tsutsui 
    551  1.30   tsutsui 		error = gdrom_read_sectors(sc, bp->b_data, bp->b_rawblkno,
    552  1.30   tsutsui 		    bp->b_bcount >> 11, &resid);
    553  1.30   tsutsui 		bp->b_error = error;
    554  1.30   tsutsui 		bp->b_resid = resid;
    555  1.30   tsutsui 		if (error != 0)
    556  1.30   tsutsui 			bp->b_resid = bp->b_bcount;
    557  1.30   tsutsui 
    558  1.32   tsutsui 		sc->is_busy = false;
    559  1.30   tsutsui 		wakeup(&sc->is_busy);
    560  1.30   tsutsui 
    561  1.30   tsutsui 		s = splbio();
    562  1.30   tsutsui 		disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
    563  1.30   tsutsui 		    (bp->b_flags & B_READ) != 0);
    564  1.30   tsutsui 		splx(s);
    565  1.30   tsutsui 		biodone(bp);
    566  1.30   tsutsui 	}
    567  1.30   tsutsui 
    568  1.30   tsutsui 	sc->is_active = false;
    569  1.30   tsutsui }
    570  1.30   tsutsui 
    571   1.1    marcus int
    572  1.20  christos gdromioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    573   1.1    marcus {
    574   1.2    marcus 	struct gdrom_softc *sc;
    575   1.2    marcus 	int unit, error;
    576  1.34   tsutsui 
    577  1.34   tsutsui 	DPRINTF(("GDROM: ioctl %lx\n", cmd));
    578   1.2    marcus 
    579   1.2    marcus 	unit = DISKUNIT(dev);
    580  1.25   tsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
    581   1.2    marcus 
    582   1.2    marcus 	switch (cmd) {
    583   1.2    marcus 	case CDIOREADMSADDR: {
    584   1.8       uch 		int s, track, sessno = *(int *)addr;
    585   1.2    marcus 		struct gd_toc toc;
    586   1.2    marcus 
    587   1.2    marcus 		if (sessno != 0)
    588  1.17   tsutsui 			return EINVAL;
    589   1.2    marcus 
    590   1.3    marcus 		s = splbio();
    591   1.8       uch 		while (sc->is_busy)
    592   1.8       uch 			tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    593  1.32   tsutsui 		sc->is_busy = true;
    594   1.3    marcus 		splx(s);
    595   1.3    marcus 
    596   1.3    marcus 		error = gdrom_read_toc(sc, &toc);
    597   1.3    marcus 
    598  1.32   tsutsui 		sc->is_busy = false;
    599   1.3    marcus 		wakeup(&sc->is_busy);
    600   1.3    marcus 
    601  1.34   tsutsui 		if (error != 0)
    602   1.8       uch 			return error;
    603  1.35       abs #ifdef GDROMDEBUGTOC
    604  1.35       abs 		{ /* Dump the GDROM TOC */
    605  1.35       abs 		unsigned char *ptr = (unsigned char *)&toc;
    606  1.35       abs 		int i;
    607  1.35       abs 
    608  1.35       abs 		printf("gdrom: TOC\n");
    609  1.35       abs 		for(i = 0; i < sizeof(toc); ++i) {
    610  1.35       abs 			printf("%02x", *ptr++);
    611  1.35       abs 			if( i%32 == 31)
    612  1.35       abs 				printf("\n");
    613  1.35       abs 			else if( i%4 == 3)
    614  1.35       abs 				printf(",");
    615  1.35       abs 		}
    616  1.35       abs 		printf("\n");
    617  1.35       abs 		}
    618  1.35       abs #endif
    619   1.2    marcus 		for (track = TOC_TRACK(toc.last);
    620   1.8       uch 		    track >= TOC_TRACK(toc.first);
    621  1.35       abs 		    --track) {
    622  1.35       abs 			if (track < 1 || track > 100)
    623  1.35       abs 				return ENXIO;
    624  1.34   tsutsui 			if (TOC_CTRL(toc.entry[track - 1]))
    625   1.8       uch 				break;
    626  1.35       abs 		}
    627   1.2    marcus 
    628  1.35       abs #ifdef GDROMDEBUGTOC
    629  1.35       abs 		printf("gdrom: Using track %d, LBA %u\n", track,
    630  1.35       abs 		    TOC_LBA(toc.entry[track - 1]));
    631  1.35       abs #endif
    632   1.2    marcus 
    633  1.34   tsutsui 		*(int *)addr = htonl(TOC_LBA(toc.entry[track - 1])) -
    634   1.8       uch 		    sc->openpart_start;
    635   1.2    marcus 
    636  1.17   tsutsui 		return 0;
    637   1.2    marcus 	}
    638   1.8       uch 	default:
    639  1.29   tsutsui 		return ENOTTY;
    640   1.2    marcus 	}
    641   1.2    marcus 
    642   1.2    marcus #ifdef DIAGNOSTIC
    643   1.2    marcus 	panic("gdromioctl: impossible");
    644   1.2    marcus #endif
    645   1.1    marcus }
    646   1.1    marcus 
    647   1.1    marcus 
    648   1.1    marcus int
    649   1.8       uch gdromread(dev_t dev, struct uio *uio, int flags)
    650   1.1    marcus {
    651  1.34   tsutsui 
    652  1.34   tsutsui 	DPRINTF(("GDROM: read\n"));
    653  1.17   tsutsui 	return physio(gdromstrategy, NULL, dev, B_READ, minphys, uio);
    654   1.1    marcus }
    655   1.1    marcus 
    656   1.1    marcus int
    657   1.8       uch gdromwrite(dev_t dev, struct uio *uio, int flags)
    658   1.1    marcus {
    659   1.8       uch 
    660  1.17   tsutsui 	return EROFS;
    661   1.1    marcus }
    662