Home | History | Annotate | Line # | Download | only in dev
gdrom.c revision 1.14
      1  1.14  jdolecek /*	$NetBSD: gdrom.c,v 1.14 2002/10/23 09:10:59 jdolecek 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.1    marcus 
     37   1.1    marcus #include <sys/param.h>
     38   1.1    marcus #include <sys/systm.h>
     39   1.1    marcus #include <sys/device.h>
     40   1.1    marcus 
     41   1.1    marcus #include <sys/buf.h>
     42   1.1    marcus #include <sys/ioctl.h>
     43   1.1    marcus #include <sys/fcntl.h>
     44   1.1    marcus #include <sys/disklabel.h>
     45   1.1    marcus #include <sys/disk.h>
     46   1.2    marcus #include <sys/cdio.h>
     47   1.3    marcus #include <sys/proc.h>
     48  1.10   gehenna #include <sys/conf.h>
     49   1.1    marcus 
     50   1.4    marcus #include <machine/sysasicvar.h>
     51   1.1    marcus 
     52   1.8       uch int	gdrommatch(struct device *, struct cfdata *, void *);
     53   1.8       uch void	gdromattach(struct device *, struct device *, void *);
     54  1.10   gehenna 
     55  1.10   gehenna dev_type_open(gdromopen);
     56  1.10   gehenna dev_type_close(gdromclose);
     57  1.10   gehenna dev_type_read(gdromread);
     58  1.10   gehenna dev_type_write(gdromwrite);
     59  1.10   gehenna dev_type_ioctl(gdromioctl);
     60  1.10   gehenna dev_type_strategy(gdromstrategy);
     61  1.10   gehenna 
     62  1.10   gehenna const struct bdevsw gdrom_bdevsw = {
     63  1.10   gehenna 	gdromopen, gdromclose, gdromstrategy, gdromioctl, nodump,
     64  1.10   gehenna 	nosize, D_DISK
     65  1.10   gehenna };
     66  1.10   gehenna 
     67  1.10   gehenna const struct cdevsw gdrom_cdevsw = {
     68  1.10   gehenna 	gdromopen, gdromclose, gdromread, gdromwrite, gdromioctl,
     69  1.14  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
     70  1.10   gehenna };
     71   1.1    marcus 
     72   1.1    marcus struct gdrom_softc {
     73   1.1    marcus 	struct device sc_dv;	/* generic device info; must come first */
     74   1.1    marcus 	struct disk dkdev;	/* generic disk info */
     75   1.1    marcus 	struct buf curbuf;	/* state of current I/O operation */
     76   1.1    marcus 
     77   1.3    marcus 	int is_open, is_busy;
     78   1.1    marcus 	int openpart_start;	/* start sector of currently open partition */
     79   1.3    marcus 
     80   1.3    marcus 	int cmd_active;
     81   1.3    marcus 	void *cmd_result_buf;	/* where to store result data (16 bit aligned) */
     82   1.3    marcus 	int cmd_result_size;	/* number of bytes allocated for buf */
     83   1.3    marcus 	int cmd_actual;		/* number of bytes actually read */
     84   1.3    marcus 	int cmd_cond;		/* resulting condition of command */
     85   1.1    marcus };
     86   1.1    marcus 
     87  1.13   thorpej CFATTACH_DECL(gdrom, sizeof(struct gdrom_softc),
     88  1.13   thorpej     gdrommatch, gdromattach, NULL, NULL);
     89   1.1    marcus 
     90   1.1    marcus struct dkdriver gdromdkdriver = { gdromstrategy };
     91   1.1    marcus 
     92   1.1    marcus extern struct cfdriver gdrom_cd;
     93   1.1    marcus 
     94   1.1    marcus 
     95   1.1    marcus struct gd_toc {
     96   1.8       uch 	unsigned int entry[99];
     97   1.8       uch 	unsigned int first, last;
     98   1.8       uch 	unsigned int leadout;
     99   1.1    marcus };
    100   1.1    marcus 
    101   1.8       uch #define TOC_LBA(n)	((n) & 0xffffff00)
    102   1.8       uch #define TOC_ADR(n)	((n) & 0x0f)
    103   1.8       uch #define TOC_CTRL(n)	(((n) & 0xf0) >> 4)
    104   1.8       uch #define TOC_TRACK(n)	(((n) & 0x0000ff00) >> 8)
    105   1.8       uch 
    106   1.8       uch #define GDROM(o)	(*(volatile unsigned char *)(0xa05f7000 + (o)))
    107   1.8       uch 
    108   1.8       uch #define GDSTATSTAT(n)	((n) & 0xf)
    109   1.8       uch #define GDSTATDISK(n)	(((n) >> 4) & 0xf)
    110   1.8       uch 
    111   1.8       uch #define GDROM_BUSY	GDROM(0x18)
    112   1.8       uch #define GDROM_DATA	(*(volatile short *) & GDROM(0x80))
    113   1.8       uch #define GDROM_REGX	GDROM(0x84)
    114   1.8       uch #define GDROM_STAT	GDROM(0x8c)
    115   1.8       uch #define GDROM_CNTLO	GDROM(0x90)
    116   1.8       uch #define GDROM_CNTHI	GDROM(0x94)
    117   1.8       uch #define GDROM_COND	GDROM(0x9c)
    118   1.8       uch 
    119   1.8       uch int	gdrom_getstat(void);
    120   1.8       uch int	gdrom_do_command(struct gdrom_softc *, void *, void *, unsigned int);
    121   1.8       uch int	gdrom_command_sense(struct gdrom_softc *, void *, void *, unsigned int);
    122   1.8       uch int	gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
    123   1.8       uch int	gdrom_read_sectors(struct gdrom_softc *, void *, int, int);
    124   1.8       uch int	gdrom_mount_disk(struct gdrom_softc *);
    125   1.8       uch int	gdrom_intr(void *);
    126   1.1    marcus 
    127   1.1    marcus int gdrom_getstat()
    128   1.1    marcus {
    129   1.1    marcus 	int s1, s2, s3;
    130   1.1    marcus 
    131   1.8       uch 	if (GDROM_BUSY & 0x80)
    132   1.8       uch 		return (-1);
    133   1.1    marcus 	s1 = GDROM_STAT;
    134   1.1    marcus 	s2 = GDROM_STAT;
    135   1.1    marcus 	s3 = GDROM_STAT;
    136   1.8       uch 	if(GDROM_BUSY & 0x80)
    137   1.8       uch 		return (-1);
    138   1.1    marcus 	if(s1 == s2)
    139   1.8       uch 		return (s1);
    140   1.1    marcus 	else if(s2 == s3)
    141   1.8       uch 		return (s2);
    142   1.1    marcus 	else
    143   1.8       uch 		return (-1);
    144   1.1    marcus }
    145   1.1    marcus 
    146   1.3    marcus int
    147   1.8       uch gdrom_intr(void *arg)
    148   1.1    marcus {
    149   1.3    marcus 	struct gdrom_softc *sc = arg;
    150   1.3    marcus 	int s, cond;
    151   1.3    marcus 
    152   1.3    marcus 	s = splbio();
    153   1.3    marcus 	cond = GDROM_COND;
    154   1.3    marcus #ifdef GDROMDEBUG
    155   1.3    marcus 	printf("GDROM: cond = %x\n", cond);
    156   1.3    marcus #endif
    157   1.3    marcus 	if(!sc->cmd_active) {
    158   1.3    marcus #ifdef GDROMDEBUG
    159   1.8       uch 		printf("GDROM: inactive IRQ!?\n");
    160   1.3    marcus #endif
    161   1.8       uch 		splx(s);
    162   1.8       uch 		return (0);
    163   1.3    marcus 	}
    164   1.3    marcus 
    165   1.3    marcus 	if((cond & 8)) {
    166   1.8       uch 		int cnt = (GDROM_CNTHI << 8) | GDROM_CNTLO;
    167   1.3    marcus #ifdef GDROMDEBUG
    168   1.3    marcus 		printf("GDROM: cnt = %d\n", cnt);
    169   1.3    marcus #endif
    170   1.3    marcus 		sc->cmd_actual += cnt;
    171   1.3    marcus 		if(cnt > 0 && sc->cmd_result_size > 0) {
    172   1.8       uch 			int subcnt = (cnt > sc->cmd_result_size ?
    173   1.8       uch 			    sc->cmd_result_size : cnt);
    174   1.3    marcus 			int16_t *ptr = sc->cmd_result_buf;
    175   1.8       uch 			sc->cmd_result_buf = ((char *)sc->cmd_result_buf) +
    176   1.8       uch 			    subcnt;
    177   1.3    marcus 			sc->cmd_result_size -= subcnt;
    178   1.3    marcus 			cnt -= subcnt;
    179   1.8       uch 			while (subcnt > 0) {
    180   1.8       uch 				*ptr++ = GDROM_DATA;
    181   1.8       uch 				subcnt -= 2;
    182   1.3    marcus 			}
    183   1.3    marcus 		}
    184   1.8       uch 		while (cnt > 0) {
    185   1.8       uch 			__volatile int16_t tmp;
    186   1.8       uch 			tmp = GDROM_DATA;
    187   1.8       uch 			cnt -= 2;
    188   1.3    marcus 		}
    189   1.3    marcus 	}
    190   1.8       uch 	while (GDROM_BUSY & 0x80);
    191   1.3    marcus 
    192   1.8       uch 	if ((cond & 8) == 0) {
    193   1.3    marcus 		sc->cmd_cond = cond;
    194   1.3    marcus 		sc->cmd_active = 0;
    195   1.3    marcus 		wakeup(&sc->cmd_active);
    196   1.3    marcus 	}
    197   1.1    marcus 
    198   1.3    marcus 	splx(s);
    199   1.8       uch 	return (0);
    200   1.1    marcus }
    201   1.1    marcus 
    202   1.3    marcus 
    203   1.8       uch int gdrom_do_command(struct gdrom_softc *sc, void *req, void *buf,
    204   1.8       uch     unsigned int nbyt)
    205   1.1    marcus {
    206   1.3    marcus 	int i, s;
    207   1.3    marcus 	short *ptr = req;
    208   1.3    marcus 
    209   1.8       uch 	while (GDROM_BUSY & 0x88)
    210   1.8       uch 		;
    211   1.8       uch 	if (buf != NULL) {
    212   1.8       uch 		GDROM_CNTLO = nbyt & 0xff;
    213   1.8       uch 		GDROM_CNTHI = (nbyt >> 8) & 0xff;
    214   1.8       uch 		GDROM_REGX = 0;
    215   1.3    marcus 	}
    216   1.3    marcus 	sc->cmd_result_buf = buf;
    217   1.3    marcus 	sc->cmd_result_size = nbyt;
    218   1.1    marcus 
    219   1.8       uch 	if (GDSTATSTAT(GDROM_STAT) == 6)
    220   1.8       uch 		return (-1);
    221   1.1    marcus 
    222   1.1    marcus 	GDROM_COND = 0xa0;
    223   1.8       uch 	for (i = 0; i < 64; i++)
    224   1.8       uch 		;
    225   1.8       uch 	while ((GDROM_BUSY & 0x88) != 8)
    226   1.8       uch 		;
    227   1.3    marcus 
    228   1.3    marcus 	s = splbio();
    229   1.3    marcus 
    230   1.3    marcus 	sc->cmd_actual = 0;
    231   1.3    marcus 	sc->cmd_active = 1;
    232   1.3    marcus 
    233   1.8       uch 	for (i = 0; i< 6; i++)
    234   1.8       uch 		GDROM_DATA = ptr[i];
    235   1.1    marcus 
    236   1.8       uch 	while (sc->cmd_active)
    237   1.8       uch 		tsleep(&sc->cmd_active, PRIBIO, "gdrom", 0);
    238   1.3    marcus 
    239   1.3    marcus 	splx(s);
    240   1.3    marcus 
    241   1.8       uch 	return (sc->cmd_cond);
    242   1.1    marcus }
    243   1.1    marcus 
    244   1.3    marcus 
    245   1.8       uch int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
    246   1.8       uch     unsigned int nbyt)
    247   1.1    marcus {
    248   1.1    marcus 	/* 76543210 76543210
    249   1.8       uch 	   0   0x13      -
    250   1.8       uch 	   2    -      bufsz(hi)
    251   1.8       uch 	   4 bufsz(lo)   -
    252   1.8       uch 	   6    -        -
    253   1.8       uch 	   8    -        -
    254   1.8       uch 	   10    -        -        */
    255   1.1    marcus 	unsigned short sense_data[5];
    256   1.1    marcus 	unsigned char cmd[12];
    257   1.1    marcus 	int sense_key, sense_specific;
    258   1.1    marcus 
    259   1.3    marcus 	int cond = gdrom_do_command(sc, req, buf, nbyt);
    260   1.3    marcus 
    261   1.8       uch 	if (cond < 0) {
    262   1.1    marcus #ifdef GDROMDEBUG
    263   1.8       uch 		printf("GDROM: not ready (2:58)\n");
    264   1.1    marcus #endif
    265   1.8       uch 		return (EIO);
    266   1.1    marcus 	}
    267   1.1    marcus 
    268   1.8       uch 	if ((cond & 1) == 0) {
    269   1.1    marcus #ifdef GDROMDEBUG
    270   1.8       uch 		printf("GDROM: no sense.  0:0\n");
    271   1.1    marcus #endif
    272   1.8       uch 		return (0);
    273   1.1    marcus 	}
    274   1.1    marcus 
    275   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    276   1.1    marcus 
    277   1.1    marcus 	cmd[0] = 0x13;
    278   1.1    marcus 	cmd[4] = sizeof(sense_data);
    279   1.1    marcus 
    280   1.3    marcus 	gdrom_do_command(sc, cmd, sense_data, sizeof(sense_data));
    281   1.1    marcus 
    282   1.1    marcus 	sense_key = sense_data[1] & 0xf;
    283   1.1    marcus 	sense_specific = sense_data[4];
    284   1.8       uch 	if (sense_key == 11 && sense_specific == 0) {
    285   1.1    marcus #ifdef GDROMDEBUG
    286   1.8       uch 		printf("GDROM: aborted (ignored).  0:0\n");
    287   1.1    marcus #endif
    288   1.8       uch 		return (0);
    289   1.1    marcus 	}
    290   1.1    marcus 
    291   1.1    marcus #ifdef GDROMDEBUG
    292   1.3    marcus 	printf("GDROM: SENSE %d:", sense_key);
    293   1.3    marcus 	printf("GDROM: %d\n", sense_specific);
    294   1.1    marcus #endif
    295   1.1    marcus 
    296   1.8       uch 	return (sense_key == 0 ? 0 : EIO);
    297   1.1    marcus }
    298   1.1    marcus 
    299   1.8       uch int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
    300   1.1    marcus {
    301   1.1    marcus 	/* 76543210 76543210
    302   1.8       uch 	   0   0x14      -
    303   1.8       uch 	   2    -      bufsz(hi)
    304   1.8       uch 	   4 bufsz(lo)   -
    305   1.8       uch 	   6    -        -
    306   1.8       uch 	   8    -        -
    307   1.8       uch 	   10    -        -        */
    308   1.1    marcus 	unsigned char cmd[12];
    309   1.1    marcus 
    310   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    311   1.1    marcus 
    312   1.1    marcus 	cmd[0] = 0x14;
    313   1.8       uch 	cmd[3] = sizeof(struct gd_toc) >> 8;
    314   1.8       uch 	cmd[4] = sizeof(struct gd_toc) & 0xff;
    315   1.1    marcus 
    316   1.8       uch 	return (gdrom_command_sense(sc, cmd, toc, sizeof(struct gd_toc)));
    317   1.1    marcus }
    318   1.1    marcus 
    319   1.8       uch int gdrom_read_sectors(struct gdrom_softc *sc, void *buf, int sector, int cnt)
    320   1.1    marcus {
    321   1.1    marcus 	/* 76543210 76543210
    322   1.8       uch 	   0   0x30    datafmt
    323   1.8       uch 	   2  sec(hi)  sec(mid)
    324   1.8       uch 	   4  sec(lo)    -
    325   1.8       uch 	   6    -        -
    326   1.8       uch 	   8  cnt(hi)  cnt(mid)
    327   1.8       uch 	   10  cnt(lo)    -        */
    328   1.1    marcus 	unsigned char cmd[12];
    329   1.1    marcus 
    330   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    331   1.1    marcus 
    332   1.1    marcus 	cmd[0] = 0x30;
    333   1.1    marcus 	cmd[1] = 0x20;
    334   1.1    marcus 	cmd[2] = sector>>16;
    335   1.1    marcus 	cmd[3] = sector>>8;
    336   1.1    marcus 	cmd[4] = sector;
    337   1.1    marcus 	cmd[8] = cnt>>16;
    338   1.1    marcus 	cmd[9] = cnt>>8;
    339   1.1    marcus 	cmd[10] = cnt;
    340   1.1    marcus 
    341   1.8       uch 	return (gdrom_command_sense(sc, cmd, buf, cnt << 11));
    342   1.1    marcus }
    343   1.1    marcus 
    344   1.8       uch int gdrom_mount_disk(struct gdrom_softc *sc)
    345   1.1    marcus {
    346   1.1    marcus 	/* 76543210 76543210
    347   1.8       uch 	   0   0x70      -
    348   1.8       uch 	   2   0x1f      -
    349   1.8       uch 	   4    -        -
    350   1.8       uch 	   6    -        -
    351   1.8       uch 	   8    -        -
    352   1.8       uch 	   10    -        -        */
    353   1.1    marcus 	unsigned char cmd[12];
    354   1.1    marcus 
    355   1.5       wiz 	memset(cmd, 0, sizeof(cmd));
    356   1.1    marcus 
    357   1.1    marcus 	cmd[0] = 0x70;
    358   1.1    marcus 	cmd[1] = 0x1f;
    359   1.1    marcus 
    360   1.8       uch 	return (gdrom_command_sense(sc, cmd, NULL, 0));
    361   1.1    marcus }
    362   1.1    marcus 
    363   1.1    marcus int
    364   1.8       uch gdrommatch(struct device *parent, struct cfdata *cf, void *aux)
    365   1.1    marcus {
    366   1.1    marcus 	static int gdrom_matched = 0;
    367   1.1    marcus 
    368   1.1    marcus 	/* Allow only once instance. */
    369  1.11   thorpej 	if (gdrom_matched)
    370   1.8       uch 		return (0);
    371   1.1    marcus 	gdrom_matched = 1;
    372   1.7       uch 
    373   1.8       uch 	return (1);
    374   1.1    marcus }
    375   1.1    marcus 
    376   1.1    marcus void
    377   1.8       uch gdromattach(struct device *parent, struct device *self, void *aux)
    378   1.1    marcus {
    379   1.1    marcus 	struct gdrom_softc *sc;
    380   1.8       uch 	u_int32_t p, x;
    381   1.1    marcus 
    382   1.8       uch 	sc = (struct gdrom_softc *)self;
    383   1.1    marcus 
    384   1.7       uch 	printf(": SH4 IRL 9\n");
    385   1.1    marcus 
    386   1.1    marcus 	/*
    387   1.1    marcus 	 * Initialize and attach the disk structure.
    388   1.1    marcus 	 */
    389   1.1    marcus 	sc->dkdev.dk_name = sc->sc_dv.dv_xname;
    390   1.1    marcus 	sc->dkdev.dk_driver = &gdromdkdriver;
    391   1.1    marcus 	disk_attach(&sc->dkdev);
    392   1.1    marcus 
    393   1.1    marcus 	/*
    394   1.1    marcus 	 * reenable disabled drive
    395   1.1    marcus 	 */
    396   1.8       uch 	*((__volatile u_int32_t *)0xa05f74e4) = 0x1fffff;
    397   1.8       uch 	for (p = 0; p < 0x200000 / 4; p++)
    398   1.8       uch 		x = ((__volatile u_int32_t *)0xa0000000)[p];
    399   1.1    marcus 
    400   1.7       uch 	sysasic_intr_establish(SYSASIC_EVENT_GDROM, gdrom_intr, sc);
    401   1.1    marcus }
    402   1.1    marcus 
    403   1.1    marcus int
    404   1.8       uch gdromopen(dev_t dev, int flags, int devtype, struct proc *p)
    405   1.1    marcus {
    406   1.1    marcus 	struct gdrom_softc *sc;
    407   1.3    marcus 	int s, error, unit, cnt;
    408   1.1    marcus 	struct gd_toc toc;
    409   1.1    marcus 
    410   1.1    marcus #ifdef GDROMDEBUG
    411   1.1    marcus 	printf("GDROM: open\n");
    412   1.1    marcus #endif
    413   1.1    marcus 
    414   1.1    marcus 	unit = DISKUNIT(dev);
    415   1.1    marcus 	if (unit >= gdrom_cd.cd_ndevs)
    416   1.1    marcus 		return (ENXIO);
    417   1.1    marcus 
    418   1.1    marcus 	sc = gdrom_cd.cd_devs[unit];
    419   1.1    marcus 	if (sc == NULL)
    420   1.1    marcus 		return (ENXIO);
    421   1.1    marcus 
    422   1.1    marcus 	if (sc->is_open)
    423   1.1    marcus 		return (EBUSY);
    424   1.1    marcus 
    425   1.3    marcus 	s = splbio();
    426   1.3    marcus 	while(sc->is_busy)
    427   1.8       uch 		tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    428   1.3    marcus 	sc->is_busy = 1;
    429   1.3    marcus 	splx(s);
    430   1.3    marcus 
    431   1.1    marcus 	for (cnt = 0; cnt < 5; cnt++)
    432   1.8       uch 		if ((error = gdrom_mount_disk(sc)) == 0)
    433   1.8       uch 			break;
    434   1.1    marcus 
    435   1.3    marcus 	if (!error)
    436   1.3    marcus 		error = gdrom_read_toc(sc, &toc);
    437   1.3    marcus 
    438   1.3    marcus 	sc->is_busy = 0;
    439   1.3    marcus 	wakeup(&sc->is_busy);
    440   1.3    marcus 
    441   1.1    marcus 	if (error)
    442   1.1    marcus 		return error;
    443   1.1    marcus 
    444   1.1    marcus 	sc->is_open = 1;
    445   1.2    marcus 	sc->openpart_start = 150;
    446   1.1    marcus 
    447   1.1    marcus #ifdef GDROMDEBUG
    448   1.3    marcus 	printf("GDROM: open OK\n");
    449   1.1    marcus #endif
    450   1.1    marcus 	return (0);
    451   1.1    marcus }
    452   1.1    marcus 
    453   1.1    marcus int
    454   1.8       uch gdromclose(dev_t dev, int flags, int devtype, struct proc *p)
    455   1.1    marcus {
    456   1.1    marcus 	struct gdrom_softc *sc;
    457   1.1    marcus 	int unit;
    458   1.1    marcus #ifdef GDROMDEBUG
    459   1.1    marcus 	printf("GDROM: close\n");
    460   1.1    marcus #endif
    461   1.1    marcus 	unit = DISKUNIT(dev);
    462   1.1    marcus 	sc = gdrom_cd.cd_devs[unit];
    463   1.1    marcus 
    464   1.1    marcus 	sc->is_open = 0;
    465   1.1    marcus 
    466   1.1    marcus 	return (0);
    467   1.1    marcus }
    468   1.1    marcus 
    469   1.1    marcus void
    470   1.8       uch gdromstrategy(struct buf *bp)
    471   1.1    marcus {
    472   1.1    marcus 	struct gdrom_softc *sc;
    473   1.3    marcus 	int s, unit, error;
    474   1.1    marcus #ifdef GDROMDEBUG
    475   1.1    marcus 	printf("GDROM: strategy\n");
    476   1.1    marcus #endif
    477   1.1    marcus 
    478   1.1    marcus 	unit = DISKUNIT(bp->b_dev);
    479   1.1    marcus 	sc = gdrom_cd.cd_devs[unit];
    480   1.1    marcus 
    481   1.1    marcus 	if (bp->b_bcount == 0)
    482   1.8       uch 		goto done;
    483   1.1    marcus 
    484   1.1    marcus 	bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
    485   1.1    marcus 
    486   1.1    marcus #ifdef GDROMDEBUG
    487   1.3    marcus 	printf("GDROM: read_sectors(%p, %d, %ld) [%ld bytes]\n",
    488   1.8       uch 	    bp->b_data, bp->b_rawblkno,
    489   1.8       uch 	    bp->b_bcount>>11, bp->b_bcount);
    490   1.1    marcus #endif
    491   1.3    marcus 	s = splbio();
    492   1.8       uch 	while (sc->is_busy)
    493   1.8       uch 		tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    494   1.3    marcus 	sc->is_busy = 1;
    495   1.3    marcus 	splx(s);
    496   1.1    marcus 
    497   1.6    atatat 	if ((error = gdrom_read_sectors(sc, bp->b_data, bp->b_rawblkno,
    498   1.8       uch 	    bp->b_bcount >> 11))) {
    499   1.8       uch 		bp->b_error = error;
    500   1.8       uch 		bp->b_flags |= B_ERROR;
    501   1.1    marcus 	}
    502   1.1    marcus 
    503   1.3    marcus 	sc->is_busy = 0;
    504   1.3    marcus 	wakeup(&sc->is_busy);
    505   1.3    marcus 
    506   1.8       uch  done:
    507   1.1    marcus 	bp->b_resid = bp->b_bcount;
    508   1.1    marcus 	biodone(bp);
    509   1.1    marcus }
    510   1.1    marcus 
    511   1.1    marcus int
    512   1.8       uch gdromioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    513   1.1    marcus {
    514   1.2    marcus 	struct gdrom_softc *sc;
    515   1.2    marcus 	int unit, error;
    516   1.1    marcus #ifdef GDROMDEBUG
    517   1.1    marcus 	printf("GDROM: ioctl %lx\n", cmd);
    518   1.1    marcus #endif
    519   1.2    marcus 
    520   1.2    marcus 	unit = DISKUNIT(dev);
    521   1.2    marcus 	sc = gdrom_cd.cd_devs[unit];
    522   1.2    marcus 
    523   1.2    marcus 	switch (cmd) {
    524   1.2    marcus 	case CDIOREADMSADDR: {
    525   1.8       uch 		int s, track, sessno = *(int *)addr;
    526   1.2    marcus 		struct gd_toc toc;
    527   1.2    marcus 
    528   1.2    marcus 		if (sessno != 0)
    529   1.2    marcus 			return (EINVAL);
    530   1.2    marcus 
    531   1.3    marcus 		s = splbio();
    532   1.8       uch 		while (sc->is_busy)
    533   1.8       uch 			tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
    534   1.3    marcus 		sc->is_busy = 1;
    535   1.3    marcus 		splx(s);
    536   1.3    marcus 
    537   1.3    marcus 		error = gdrom_read_toc(sc, &toc);
    538   1.3    marcus 
    539   1.3    marcus 		sc->is_busy = 0;
    540   1.3    marcus 		wakeup(&sc->is_busy);
    541   1.3    marcus 
    542   1.3    marcus 		if (error)
    543   1.8       uch 			return error;
    544   1.2    marcus 
    545   1.2    marcus 		for (track = TOC_TRACK(toc.last);
    546   1.8       uch 		    track >= TOC_TRACK(toc.first);
    547   1.8       uch 		    --track)
    548   1.8       uch 			if (TOC_CTRL(toc.entry[track-1]))
    549   1.8       uch 				break;
    550   1.2    marcus 
    551   1.2    marcus 		if (track < TOC_TRACK(toc.first) || track > 100)
    552   1.8       uch 			return (ENXIO);
    553   1.2    marcus 
    554   1.8       uch 		*(int *)addr = htonl(TOC_LBA(toc.entry[track-1])) -
    555   1.8       uch 		    sc->openpart_start;
    556   1.2    marcus 
    557   1.8       uch 		return (0);
    558   1.2    marcus 	}
    559   1.8       uch 	default:
    560   1.8       uch 		return (EINVAL);
    561   1.2    marcus 	}
    562   1.2    marcus 
    563   1.2    marcus #ifdef DIAGNOSTIC
    564   1.2    marcus 	panic("gdromioctl: impossible");
    565   1.2    marcus #endif
    566   1.1    marcus }
    567   1.1    marcus 
    568   1.1    marcus 
    569   1.1    marcus int
    570   1.8       uch gdromread(dev_t dev, struct uio *uio, int flags)
    571   1.1    marcus {
    572   1.1    marcus #ifdef GDROMDEBUG
    573   1.1    marcus 	printf("GDROM: read\n");
    574   1.1    marcus #endif
    575   1.1    marcus 	return (physio(gdromstrategy, NULL, dev, B_READ, minphys, uio));
    576   1.1    marcus }
    577   1.1    marcus 
    578   1.1    marcus int
    579   1.8       uch gdromwrite(dev_t dev, struct uio *uio, int flags)
    580   1.1    marcus {
    581   1.8       uch 
    582   1.1    marcus 	return (EROFS);
    583   1.1    marcus }
    584