xref: /netbsd-src/sys/arch/dreamcast/dev/gdrom.c (revision 32829346f4ce1c2951c75c9696c61f181d2de120)
1*32829346Schristos /*	$NetBSD: gdrom.c,v 1.42 2016/03/13 17:59:20 christos Exp $	*/
236382f48Smarcus 
336382f48Smarcus /*-
436382f48Smarcus  * Copyright (c) 2001 Marcus Comstedt
536382f48Smarcus  * All rights reserved.
636382f48Smarcus  *
736382f48Smarcus  * Redistribution and use in source and binary forms, with or without
836382f48Smarcus  * modification, are permitted provided that the following conditions
936382f48Smarcus  * are met:
1036382f48Smarcus  * 1. Redistributions of source code must retain the above copyright
1136382f48Smarcus  *    notice, this list of conditions and the following disclaimer.
1236382f48Smarcus  * 2. Redistributions in binary form must reproduce the above copyright
1336382f48Smarcus  *    notice, this list of conditions and the following disclaimer in the
1436382f48Smarcus  *    documentation and/or other materials provided with the distribution.
1536382f48Smarcus  * 3. All advertising materials mentioning features or use of this software
1636382f48Smarcus  *    must display the following acknowledgement:
1736382f48Smarcus  *	This product includes software developed by Marcus Comstedt.
1836382f48Smarcus  * 4. Neither the name of The NetBSD Foundation nor the names of its
1936382f48Smarcus  *    contributors may be used to endorse or promote products derived
2036382f48Smarcus  *    from this software without specific prior written permission.
2136382f48Smarcus  *
2236382f48Smarcus  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2336382f48Smarcus  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2436382f48Smarcus  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2536382f48Smarcus  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2636382f48Smarcus  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2736382f48Smarcus  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2836382f48Smarcus  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2936382f48Smarcus  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3036382f48Smarcus  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3136382f48Smarcus  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3236382f48Smarcus  * POSSIBILITY OF SUCH DAMAGE.
3336382f48Smarcus  */
3436382f48Smarcus 
3536382f48Smarcus #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
36*32829346Schristos __KERNEL_RCSID(0, "$NetBSD: gdrom.c,v 1.42 2016/03/13 17:59:20 christos Exp $");
3736382f48Smarcus 
3836382f48Smarcus #include <sys/param.h>
3936382f48Smarcus #include <sys/systm.h>
4036382f48Smarcus #include <sys/device.h>
4136382f48Smarcus 
4236382f48Smarcus #include <sys/buf.h>
4368a1ead9Stsutsui #include <sys/bufq.h>
4436382f48Smarcus #include <sys/ioctl.h>
4536382f48Smarcus #include <sys/fcntl.h>
4636382f48Smarcus #include <sys/disklabel.h>
4736382f48Smarcus #include <sys/disk.h>
487df82ad1Smarcus #include <sys/cdio.h>
497d1d4a2fSmarcus #include <sys/proc.h>
5077a6b82bSgehenna #include <sys/conf.h>
5136382f48Smarcus 
52bc069b93Smarcus #include <machine/sysasicvar.h>
5336382f48Smarcus 
54287b6acaStsutsui #include "ioconf.h"
55287b6acaStsutsui 
56cea58592Stsutsui static int  gdrommatch(device_t, cfdata_t, void *);
57cea58592Stsutsui static void gdromattach(device_t, device_t, void *);
5877a6b82bSgehenna 
5977a6b82bSgehenna dev_type_open(gdromopen);
6077a6b82bSgehenna dev_type_close(gdromclose);
6177a6b82bSgehenna dev_type_read(gdromread);
6277a6b82bSgehenna dev_type_write(gdromwrite);
6377a6b82bSgehenna dev_type_ioctl(gdromioctl);
6477a6b82bSgehenna dev_type_strategy(gdromstrategy);
6577a6b82bSgehenna 
6677a6b82bSgehenna const struct bdevsw gdrom_bdevsw = {
67a68f9396Sdholland 	.d_open = gdromopen,
68a68f9396Sdholland 	.d_close = gdromclose,
692d45390aSmartin 	.d_strategy = gdromstrategy,
70a68f9396Sdholland 	.d_ioctl = gdromioctl,
71a68f9396Sdholland 	.d_dump = nodump,
72a68f9396Sdholland 	.d_psize = nosize,
738c70ef39Sdholland 	.d_discard = nodiscard,
74a68f9396Sdholland 	.d_flag = D_DISK
7577a6b82bSgehenna };
7677a6b82bSgehenna 
7777a6b82bSgehenna const struct cdevsw gdrom_cdevsw = {
78a68f9396Sdholland 	.d_open = gdromopen,
79a68f9396Sdholland 	.d_close = gdromclose,
80a68f9396Sdholland 	.d_read = gdromread,
81a68f9396Sdholland 	.d_write = gdromwrite,
82a68f9396Sdholland 	.d_ioctl = gdromioctl,
83a68f9396Sdholland 	.d_stop = nostop,
84a68f9396Sdholland 	.d_tty = notty,
85a68f9396Sdholland 	.d_poll = nopoll,
86a68f9396Sdholland 	.d_mmap = nommap,
87a68f9396Sdholland 	.d_kqfilter = nokqfilter,
88f9228f42Sdholland 	.d_discard = nodiscard,
89a68f9396Sdholland 	.d_flag = D_DISK
9077a6b82bSgehenna };
9136382f48Smarcus 
9236382f48Smarcus struct gdrom_softc {
93287b6acaStsutsui 	device_t sc_dev;	/* generic device info */
9468a1ead9Stsutsui 	struct disk sc_dk;	/* generic disk info */
9568a1ead9Stsutsui 	struct bufq_state *sc_bufq;	/* device buffer queue */
9636382f48Smarcus 	struct buf curbuf;	/* state of current I/O operation */
9736382f48Smarcus 
98a90704b5Stsutsui 	bool is_open;
99a90704b5Stsutsui 	bool is_busy;
10068a1ead9Stsutsui 	bool is_active;
10136382f48Smarcus 	int openpart_start;	/* start sector of currently open partition */
1027d1d4a2fSmarcus 
1037d1d4a2fSmarcus 	int cmd_active;
1047d1d4a2fSmarcus 	void *cmd_result_buf;	/* where to store result data (16 bit aligned) */
1057d1d4a2fSmarcus 	int cmd_result_size;	/* number of bytes allocated for buf */
1067d1d4a2fSmarcus 	int cmd_actual;		/* number of bytes actually read */
1077d1d4a2fSmarcus 	int cmd_cond;		/* resulting condition of command */
10836382f48Smarcus };
10936382f48Smarcus 
110287b6acaStsutsui CFATTACH_DECL_NEW(gdrom, sizeof(struct gdrom_softc),
111c5e91d44Sthorpej     gdrommatch, gdromattach, NULL, NULL);
11236382f48Smarcus 
1136f00c789Smlelstv struct dkdriver gdromdkdriver = {
1146f00c789Smlelstv 	.d_strategy = gdromstrategy
1156f00c789Smlelstv };
11636382f48Smarcus 
11736382f48Smarcus 
11836382f48Smarcus struct gd_toc {
11936382f48Smarcus 	unsigned int entry[99];
12036382f48Smarcus 	unsigned int first, last;
12136382f48Smarcus 	unsigned int leadout;
12236382f48Smarcus };
12336382f48Smarcus 
124f2d6cdb4Stsutsui #ifdef GDROMDEBUG
125f2d6cdb4Stsutsui #define DPRINTF(x)	printf x
126f2d6cdb4Stsutsui #else
127f2d6cdb4Stsutsui #define DPRINTF(x)	/**/
128f2d6cdb4Stsutsui #endif
129f2d6cdb4Stsutsui 
13036382f48Smarcus #define TOC_LBA(n)	((n) & 0xffffff00)
13136382f48Smarcus #define TOC_ADR(n)	((n) & 0x0f)
13236382f48Smarcus #define TOC_CTRL(n)	(((n) & 0xf0) >> 4)
13336382f48Smarcus #define TOC_TRACK(n)	(((n) & 0x0000ff00) >> 8)
13436382f48Smarcus 
135*32829346Schristos #define GDROM(o)	(*(volatile uint8_t *)(0xa05f7000U + (o)))
13636382f48Smarcus 
13736382f48Smarcus #define GDSTATSTAT(n)	((n) & 0xf)
13836382f48Smarcus #define GDSTATDISK(n)	(((n) >> 4) & 0xf)
13936382f48Smarcus 
14036382f48Smarcus #define GDROM_BUSY	GDROM(0x18)
141a90704b5Stsutsui #define GDROM_DATA	(*(volatile uint16_t *)(&GDROM(0x80)))
14236382f48Smarcus #define GDROM_REGX	GDROM(0x84)
14336382f48Smarcus #define GDROM_STAT	GDROM(0x8c)
14436382f48Smarcus #define GDROM_CNTLO	GDROM(0x90)
14536382f48Smarcus #define GDROM_CNTHI	GDROM(0x94)
14636382f48Smarcus #define GDROM_COND	GDROM(0x9c)
14736382f48Smarcus 
148cea58592Stsutsui #if 0
149cea58592Stsutsui static int gdrom_getstat(void);
150cea58592Stsutsui #endif
151cea58592Stsutsui static int gdrom_do_command(struct gdrom_softc *, void *, void *,
152cea58592Stsutsui     unsigned int, int *);
153cea58592Stsutsui static int gdrom_command_sense(struct gdrom_softc *, void *, void *,
154cea58592Stsutsui     unsigned int, int *);
155cea58592Stsutsui static int gdrom_read_toc(struct gdrom_softc *, struct gd_toc *);
156cea58592Stsutsui static int gdrom_read_sectors(struct gdrom_softc *, void *, int, int,
1571f68cc81Stsutsui     int *);
158cea58592Stsutsui static int gdrom_mount_disk(struct gdrom_softc *);
159cea58592Stsutsui static int gdrom_intr(void *);
160cea58592Stsutsui static void gdrom_start(struct gdrom_softc *);
16136382f48Smarcus 
162cea58592Stsutsui #if 0
163cea58592Stsutsui int
164cea58592Stsutsui gdrom_getstat(void)
16536382f48Smarcus {
166a90704b5Stsutsui 	uint8_t s1, s2, s3;
16736382f48Smarcus 
168b095a0d0Such 	if (GDROM_BUSY & 0x80)
169cb6453dbStsutsui 		return -1;
17036382f48Smarcus 	s1 = GDROM_STAT;
17136382f48Smarcus 	s2 = GDROM_STAT;
17236382f48Smarcus 	s3 = GDROM_STAT;
173b095a0d0Such 	if (GDROM_BUSY & 0x80)
174cb6453dbStsutsui 		return -1;
17536382f48Smarcus 	if (s1 == s2)
176cb6453dbStsutsui 		return s1;
17736382f48Smarcus 	else if (s2 == s3)
178cb6453dbStsutsui 		return s2;
17936382f48Smarcus 	else
180cb6453dbStsutsui 		return -1;
18136382f48Smarcus }
182cea58592Stsutsui #endif
18336382f48Smarcus 
1847d1d4a2fSmarcus int
gdrom_intr(void * arg)185b095a0d0Such gdrom_intr(void *arg)
18636382f48Smarcus {
1877d1d4a2fSmarcus 	struct gdrom_softc *sc = arg;
188a90704b5Stsutsui 	int s;
189a90704b5Stsutsui 	uint8_t cond;
19036382f48Smarcus 
1917d1d4a2fSmarcus 	s = splbio();
1927d1d4a2fSmarcus 	cond = GDROM_COND;
193f2d6cdb4Stsutsui 	DPRINTF(("GDROM: cond = %x\n", cond));
1947d1d4a2fSmarcus 	if (!sc->cmd_active) {
195f2d6cdb4Stsutsui 		DPRINTF(("GDROM: inactive IRQ!?\n"));
1967d1d4a2fSmarcus 		splx(s);
197cb6453dbStsutsui 		return 0;
19836382f48Smarcus 	}
19936382f48Smarcus 
200f2d6cdb4Stsutsui 	if ((cond & 0x08) != 0) {
2017d1d4a2fSmarcus 		int cnt = (GDROM_CNTHI << 8) | GDROM_CNTLO;
202f2d6cdb4Stsutsui 		DPRINTF(("GDROM: cnt = %d\n", cnt));
2037d1d4a2fSmarcus 		sc->cmd_actual += cnt;
2047d1d4a2fSmarcus 		if (cnt > 0 && sc->cmd_result_size > 0) {
2057d1d4a2fSmarcus 			int subcnt = (cnt > sc->cmd_result_size ?
2067d1d4a2fSmarcus 			    sc->cmd_result_size : cnt);
207a90704b5Stsutsui 			uint16_t *ptr = sc->cmd_result_buf;
208a90704b5Stsutsui 			sc->cmd_result_buf = ((uint8_t *)sc->cmd_result_buf) +
209b095a0d0Such 			    subcnt;
2107d1d4a2fSmarcus 			sc->cmd_result_size -= subcnt;
2117d1d4a2fSmarcus 			cnt -= subcnt;
2127d1d4a2fSmarcus 			while (subcnt > 0) {
2137d1d4a2fSmarcus 				*ptr++ = GDROM_DATA;
2147d1d4a2fSmarcus 				subcnt -= 2;
2157d1d4a2fSmarcus 			}
2167d1d4a2fSmarcus 		}
2177d1d4a2fSmarcus 		while (cnt > 0) {
218a90704b5Stsutsui 			(void)GDROM_DATA;
2197d1d4a2fSmarcus 			cnt -= 2;
2207d1d4a2fSmarcus 		}
2217d1d4a2fSmarcus 	}
222f2d6cdb4Stsutsui 	while ((GDROM_BUSY & 0x80) != 0);
2237d1d4a2fSmarcus 
224f2d6cdb4Stsutsui 	if ((cond & 0x08) == 0) {
2257d1d4a2fSmarcus 		sc->cmd_cond = cond;
2267d1d4a2fSmarcus 		sc->cmd_active = 0;
2277d1d4a2fSmarcus 		wakeup(&sc->cmd_active);
2287d1d4a2fSmarcus 	}
2297d1d4a2fSmarcus 
2307d1d4a2fSmarcus 	splx(s);
231cb6453dbStsutsui 	return 1;
2327d1d4a2fSmarcus }
2337d1d4a2fSmarcus 
2347d1d4a2fSmarcus 
235f2d6cdb4Stsutsui int
gdrom_do_command(struct gdrom_softc * sc,void * req,void * buf,unsigned int nbyt,int * resid)236f2d6cdb4Stsutsui gdrom_do_command(struct gdrom_softc *sc, void *req, void *buf,
2371f68cc81Stsutsui     unsigned int nbyt, int *resid)
23836382f48Smarcus {
2397d1d4a2fSmarcus 	int i, s;
240a90704b5Stsutsui 	uint16_t *ptr = req;
2417d1d4a2fSmarcus 
242b095a0d0Such 	while (GDROM_BUSY & 0x88)
243b095a0d0Such 		;
2447d1d4a2fSmarcus 	if (buf != NULL) {
2457d1d4a2fSmarcus 		GDROM_CNTLO = nbyt & 0xff;
2467d1d4a2fSmarcus 		GDROM_CNTHI = (nbyt >> 8) & 0xff;
2477d1d4a2fSmarcus 		GDROM_REGX = 0;
2487d1d4a2fSmarcus 	}
2497d1d4a2fSmarcus 	sc->cmd_result_buf = buf;
2507d1d4a2fSmarcus 	sc->cmd_result_size = nbyt;
25136382f48Smarcus 
252f2d6cdb4Stsutsui 	if (GDSTATSTAT(GDROM_STAT) == 0x06)
253cb6453dbStsutsui 		return -1;
25436382f48Smarcus 
25536382f48Smarcus 	GDROM_COND = 0xa0;
256938f3699Stsutsui 	DELAY(1);
257f2d6cdb4Stsutsui 	while ((GDROM_BUSY & 0x88) != 0x08)
258b095a0d0Such 		;
2597d1d4a2fSmarcus 
2607d1d4a2fSmarcus 	s = splbio();
2617d1d4a2fSmarcus 
2627d1d4a2fSmarcus 	sc->cmd_actual = 0;
2637d1d4a2fSmarcus 	sc->cmd_active = 1;
2647d1d4a2fSmarcus 
26536382f48Smarcus 	for (i = 0; i < 6; i++)
26636382f48Smarcus 		GDROM_DATA = ptr[i];
26736382f48Smarcus 
2687d1d4a2fSmarcus 	while (sc->cmd_active)
2697d1d4a2fSmarcus 		tsleep(&sc->cmd_active, PRIBIO, "gdrom", 0);
2707d1d4a2fSmarcus 
2717d1d4a2fSmarcus 	splx(s);
2727d1d4a2fSmarcus 
2731f68cc81Stsutsui 	if (resid != NULL)
2741f68cc81Stsutsui 		*resid = sc->cmd_result_size;
2751f68cc81Stsutsui 
276cb6453dbStsutsui 	return sc->cmd_cond;
27736382f48Smarcus }
27836382f48Smarcus 
2797d1d4a2fSmarcus 
gdrom_command_sense(struct gdrom_softc * sc,void * req,void * buf,unsigned int nbyt,int * resid)280b095a0d0Such int gdrom_command_sense(struct gdrom_softc *sc, void *req, void *buf,
2811f68cc81Stsutsui     unsigned int nbyt, int *resid)
28236382f48Smarcus {
283f2d6cdb4Stsutsui 	/*
284f2d6cdb4Stsutsui 	 *  76543210 76543210
285f2d6cdb4Stsutsui 	 *  0   0x13      -
286f2d6cdb4Stsutsui 	 *  2    -      bufsz(hi)
287f2d6cdb4Stsutsui 	 *  4 bufsz(lo)   -
288f2d6cdb4Stsutsui 	 *  6    -        -
289f2d6cdb4Stsutsui 	 *  8    -        -
290f2d6cdb4Stsutsui 	 * 10    -        -
291f2d6cdb4Stsutsui 	 */
292a90704b5Stsutsui 	uint16_t sense_data[5];
293a90704b5Stsutsui 	uint8_t cmd[12];
294f2d6cdb4Stsutsui 	int cond, sense_key, sense_specific;
29536382f48Smarcus 
296f2d6cdb4Stsutsui 	cond = gdrom_do_command(sc, req, buf, nbyt, resid);
2977d1d4a2fSmarcus 
29836382f48Smarcus 	if (cond < 0) {
299f2d6cdb4Stsutsui 		DPRINTF(("GDROM: not ready (2:58)\n"));
300cb6453dbStsutsui 		return EIO;
30136382f48Smarcus 	}
30236382f48Smarcus 
303b095a0d0Such 	if ((cond & 1) == 0) {
304f2d6cdb4Stsutsui 		DPRINTF(("GDROM: no sense.  0:0\n"));
305cb6453dbStsutsui 		return 0;
30636382f48Smarcus 	}
30736382f48Smarcus 
3088eaf0a10Swiz 	memset(cmd, 0, sizeof(cmd));
30936382f48Smarcus 
31036382f48Smarcus 	cmd[0] = 0x13;
31136382f48Smarcus 	cmd[4] = sizeof(sense_data);
31236382f48Smarcus 
3131f68cc81Stsutsui 	gdrom_do_command(sc, cmd, sense_data, sizeof(sense_data), NULL);
31436382f48Smarcus 
31536382f48Smarcus 	sense_key = sense_data[1] & 0xf;
31636382f48Smarcus 	sense_specific = sense_data[4];
31736382f48Smarcus 	if (sense_key == 11 && sense_specific == 0) {
318f2d6cdb4Stsutsui 		DPRINTF(("GDROM: aborted (ignored).  0:0\n"));
319cb6453dbStsutsui 		return 0;
32036382f48Smarcus 	}
32136382f48Smarcus 
322f2d6cdb4Stsutsui 	DPRINTF(("GDROM: SENSE %d:", sense_key));
323f2d6cdb4Stsutsui 	DPRINTF(("GDROM: %d\n", sense_specific));
32436382f48Smarcus 
325cb6453dbStsutsui 	return sense_key == 0 ? 0 : EIO;
32636382f48Smarcus }
32736382f48Smarcus 
gdrom_read_toc(struct gdrom_softc * sc,struct gd_toc * toc)328b095a0d0Such int gdrom_read_toc(struct gdrom_softc *sc, struct gd_toc *toc)
32936382f48Smarcus {
330f2d6cdb4Stsutsui 	/*
331f2d6cdb4Stsutsui 	 *  76543210 76543210
332f2d6cdb4Stsutsui 	 *  0   0x14      -
333f2d6cdb4Stsutsui 	 *  2    -      bufsz(hi)
334f2d6cdb4Stsutsui 	 *  4 bufsz(lo)   -
335f2d6cdb4Stsutsui 	 *  6    -        -
336f2d6cdb4Stsutsui 	 *  8    -        -
337f2d6cdb4Stsutsui 	 * 10    -        -
338f2d6cdb4Stsutsui 	 */
339a90704b5Stsutsui 	uint8_t cmd[12];
34036382f48Smarcus 
3418eaf0a10Swiz 	memset(cmd, 0, sizeof(cmd));
34236382f48Smarcus 
34336382f48Smarcus 	cmd[0] = 0x14;
34436382f48Smarcus 	cmd[3] = sizeof(struct gd_toc) >> 8;
34536382f48Smarcus 	cmd[4] = sizeof(struct gd_toc) & 0xff;
34636382f48Smarcus 
3471f68cc81Stsutsui 	return gdrom_command_sense(sc, cmd, toc, sizeof(struct gd_toc), NULL);
34836382f48Smarcus }
34936382f48Smarcus 
gdrom_read_sectors(struct gdrom_softc * sc,void * buf,int sector,int cnt,int * resid)3501f68cc81Stsutsui int gdrom_read_sectors(struct gdrom_softc *sc, void *buf, int sector, int cnt,
3511f68cc81Stsutsui     int *resid)
35236382f48Smarcus {
353f2d6cdb4Stsutsui 	/*
354f2d6cdb4Stsutsui 	 *  76543210 76543210
355f2d6cdb4Stsutsui 	 *  0   0x30    datafmt
356f2d6cdb4Stsutsui 	 *  2  sec(hi)  sec(mid)
357f2d6cdb4Stsutsui 	 *  4  sec(lo)    -
358f2d6cdb4Stsutsui 	 *  6    -        -
359f2d6cdb4Stsutsui 	 *  8  cnt(hi)  cnt(mid)
360f2d6cdb4Stsutsui 	 * 10  cnt(lo)    -
361f2d6cdb4Stsutsui 	 */
362a90704b5Stsutsui 	uint8_t cmd[12];
36336382f48Smarcus 
3648eaf0a10Swiz 	memset(cmd, 0, sizeof(cmd));
36536382f48Smarcus 
36636382f48Smarcus 	cmd[0]  = 0x30;
36736382f48Smarcus 	cmd[1]  = 0x20;
36836382f48Smarcus 	cmd[2]  = sector >> 16;
36936382f48Smarcus 	cmd[3]  = sector >>  8;
37036382f48Smarcus 	cmd[4]  = sector;
37136382f48Smarcus 	cmd[8]  = cnt >> 16;
37236382f48Smarcus 	cmd[9]  = cnt >>  8;
37336382f48Smarcus 	cmd[10] = cnt;
37436382f48Smarcus 
3751f68cc81Stsutsui 	return gdrom_command_sense(sc, cmd, buf, cnt << 11, resid);
37636382f48Smarcus }
37736382f48Smarcus 
gdrom_mount_disk(struct gdrom_softc * sc)378b095a0d0Such int gdrom_mount_disk(struct gdrom_softc *sc)
37936382f48Smarcus {
380f2d6cdb4Stsutsui 	/*
381f2d6cdb4Stsutsui 	 *  76543210 76543210
382f2d6cdb4Stsutsui 	 *  0   0x70      -
383f2d6cdb4Stsutsui 	 *  2   0x1f      -
384f2d6cdb4Stsutsui 	 *  4    -        -
385f2d6cdb4Stsutsui 	 *  6    -        -
386f2d6cdb4Stsutsui 	 *  8    -        -
387f2d6cdb4Stsutsui 	 * 10    -        -
388f2d6cdb4Stsutsui 	 */
389a90704b5Stsutsui 	uint8_t cmd[12];
39036382f48Smarcus 
3918eaf0a10Swiz 	memset(cmd, 0, sizeof(cmd));
39236382f48Smarcus 
39336382f48Smarcus 	cmd[0] = 0x70;
39436382f48Smarcus 	cmd[1] = 0x1f;
39536382f48Smarcus 
3961f68cc81Stsutsui 	return gdrom_command_sense(sc, cmd, NULL, 0, NULL);
39736382f48Smarcus }
39836382f48Smarcus 
39936382f48Smarcus int
gdrommatch(device_t parent,cfdata_t cf,void * aux)400287b6acaStsutsui gdrommatch(device_t parent, cfdata_t cf, void *aux)
40136382f48Smarcus {
40236382f48Smarcus 	static int gdrom_matched = 0;
40336382f48Smarcus 
40436382f48Smarcus 	/* Allow only once instance. */
405bb34bc8cSthorpej 	if (gdrom_matched)
406cb6453dbStsutsui 		return 0;
40736382f48Smarcus 	gdrom_matched = 1;
408e8300f36Such 
409cb6453dbStsutsui 	return 1;
41036382f48Smarcus }
41136382f48Smarcus 
41236382f48Smarcus void
gdromattach(device_t parent,device_t self,void * aux)413287b6acaStsutsui gdromattach(device_t parent, device_t self, void *aux)
41436382f48Smarcus {
41536382f48Smarcus 	struct gdrom_softc *sc;
4167046111bSmartin 	uint32_t p;
41736382f48Smarcus 
418287b6acaStsutsui 	sc = device_private(self);
419287b6acaStsutsui 	sc->sc_dev = self;
42036382f48Smarcus 
42168a1ead9Stsutsui 	bufq_alloc(&sc->sc_bufq, "disksort", BUFQ_SORT_RAWBLOCK);
42268a1ead9Stsutsui 
42336382f48Smarcus 	/*
42436382f48Smarcus 	 * Initialize and attach the disk structure.
42536382f48Smarcus 	 */
42668a1ead9Stsutsui 	disk_init(&sc->sc_dk, device_xname(self), &gdromdkdriver);
42768a1ead9Stsutsui 	disk_attach(&sc->sc_dk);
42836382f48Smarcus 
42936382f48Smarcus 	/*
43036382f48Smarcus 	 * reenable disabled drive
43136382f48Smarcus 	 */
4325f1c88d7Sperry 	*((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
43336382f48Smarcus 	for (p = 0; p < 0x200000 / 4; p++)
4347046111bSmartin 		(void)((volatile uint32_t *)0xa0000000)[p];
43536382f48Smarcus 
436b66028edSmarcus 	printf(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
4370b5d7fc8Stsutsui 	sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
4380b5d7fc8Stsutsui 	    gdrom_intr, sc);
43936382f48Smarcus }
44036382f48Smarcus 
44136382f48Smarcus int
gdromopen(dev_t dev,int flags,int devtype,struct lwp * l)44295e1ffb1Schristos gdromopen(dev_t dev, int flags, int devtype, struct lwp *l)
44336382f48Smarcus {
44436382f48Smarcus 	struct gdrom_softc *sc;
4457d1d4a2fSmarcus 	int s, error, unit, cnt;
44636382f48Smarcus 	struct gd_toc toc;
44736382f48Smarcus 
448f2d6cdb4Stsutsui 	DPRINTF(("GDROM: open\n"));
44936382f48Smarcus 
45036382f48Smarcus 	unit = DISKUNIT(dev);
45136382f48Smarcus 
452e6a3083cStsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
45336382f48Smarcus 	if (sc == NULL)
454cb6453dbStsutsui 		return ENXIO;
45536382f48Smarcus 
45636382f48Smarcus 	if (sc->is_open)
457cb6453dbStsutsui 		return EBUSY;
45836382f48Smarcus 
4597d1d4a2fSmarcus 	s = splbio();
4607d1d4a2fSmarcus 	while (sc->is_busy)
4617d1d4a2fSmarcus 		tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
462a90704b5Stsutsui 	sc->is_busy = true;
4637d1d4a2fSmarcus 	splx(s);
4647d1d4a2fSmarcus 
46536382f48Smarcus 	for (cnt = 0; cnt < 5; cnt++)
4667d1d4a2fSmarcus 		if ((error = gdrom_mount_disk(sc)) == 0)
46736382f48Smarcus 			break;
46836382f48Smarcus 
469f2d6cdb4Stsutsui 	if (error == 0)
4707d1d4a2fSmarcus 		error = gdrom_read_toc(sc, &toc);
47136382f48Smarcus 
472a90704b5Stsutsui 	sc->is_busy = false;
4737d1d4a2fSmarcus 	wakeup(&sc->is_busy);
4747d1d4a2fSmarcus 
475f2d6cdb4Stsutsui 	if (error != 0)
47636382f48Smarcus 		return error;
47736382f48Smarcus 
478a90704b5Stsutsui 	sc->is_open = true;
4797df82ad1Smarcus 	sc->openpart_start = 150;
48036382f48Smarcus 
481f2d6cdb4Stsutsui 	DPRINTF(("GDROM: open OK\n"));
482cb6453dbStsutsui 	return 0;
48336382f48Smarcus }
48436382f48Smarcus 
48536382f48Smarcus int
gdromclose(dev_t dev,int flags,int devtype,struct lwp * l)48695e1ffb1Schristos gdromclose(dev_t dev, int flags, int devtype, struct lwp *l)
48736382f48Smarcus {
48836382f48Smarcus 	struct gdrom_softc *sc;
48936382f48Smarcus 	int unit;
490f2d6cdb4Stsutsui 
491f2d6cdb4Stsutsui 	DPRINTF(("GDROM: close\n"));
492f2d6cdb4Stsutsui 
49336382f48Smarcus 	unit = DISKUNIT(dev);
494e6a3083cStsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
49536382f48Smarcus 
496a90704b5Stsutsui 	sc->is_open = false;
49736382f48Smarcus 
498cb6453dbStsutsui 	return 0;
49936382f48Smarcus }
50036382f48Smarcus 
50136382f48Smarcus void
gdromstrategy(struct buf * bp)502b095a0d0Such gdromstrategy(struct buf *bp)
50336382f48Smarcus {
50436382f48Smarcus 	struct gdrom_softc *sc;
50568a1ead9Stsutsui 	int s, unit;
506f2d6cdb4Stsutsui 
507f2d6cdb4Stsutsui 	DPRINTF(("GDROM: strategy\n"));
50836382f48Smarcus 
50936382f48Smarcus 	unit = DISKUNIT(bp->b_dev);
510e6a3083cStsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
51136382f48Smarcus 
51236382f48Smarcus 	if (bp->b_bcount == 0)
51336382f48Smarcus 		goto done;
51436382f48Smarcus 
51536382f48Smarcus 	bp->b_rawblkno = bp->b_blkno / (2048 / DEV_BSIZE) + sc->openpart_start;
51636382f48Smarcus 
517f2d6cdb4Stsutsui 	DPRINTF(("GDROM: read_sectors(%p, %lld, %d) [%d bytes]\n",
51836382f48Smarcus 	    bp->b_data, bp->b_rawblkno,
519f2d6cdb4Stsutsui 	    bp->b_bcount >> 11, bp->b_bcount));
520f2d6cdb4Stsutsui 
5217d1d4a2fSmarcus 	s = splbio();
52268a1ead9Stsutsui 	bufq_put(sc->sc_bufq, bp);
5237d1d4a2fSmarcus 	splx(s);
52468a1ead9Stsutsui 	if (!sc->is_active)
52568a1ead9Stsutsui 		gdrom_start(sc);
5261f68cc81Stsutsui 	return;
5277d1d4a2fSmarcus 
52836382f48Smarcus  done:
52936382f48Smarcus 	bp->b_resid = bp->b_bcount;
53036382f48Smarcus 	biodone(bp);
53136382f48Smarcus }
53236382f48Smarcus 
53368a1ead9Stsutsui void
gdrom_start(struct gdrom_softc * sc)53468a1ead9Stsutsui gdrom_start(struct gdrom_softc *sc)
53568a1ead9Stsutsui {
53668a1ead9Stsutsui 	struct buf *bp;
53768a1ead9Stsutsui 	int error, resid, s;
53868a1ead9Stsutsui 
53968a1ead9Stsutsui 	sc->is_active = true;
54068a1ead9Stsutsui 
54168a1ead9Stsutsui 	for (;;) {
54268a1ead9Stsutsui 		s = splbio();
54368a1ead9Stsutsui 		bp = bufq_get(sc->sc_bufq);
54468a1ead9Stsutsui 		if (bp == NULL) {
54568a1ead9Stsutsui 			splx(s);
54668a1ead9Stsutsui 			break;
54768a1ead9Stsutsui 		}
54868a1ead9Stsutsui 
54968a1ead9Stsutsui 		while (sc->is_busy)
55068a1ead9Stsutsui 			tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
551a90704b5Stsutsui 		sc->is_busy = true;
55268a1ead9Stsutsui 		disk_busy(&sc->sc_dk);
55368a1ead9Stsutsui 		splx(s);
55468a1ead9Stsutsui 
55568a1ead9Stsutsui 		error = gdrom_read_sectors(sc, bp->b_data, bp->b_rawblkno,
55668a1ead9Stsutsui 		    bp->b_bcount >> 11, &resid);
55768a1ead9Stsutsui 		bp->b_error = error;
55868a1ead9Stsutsui 		bp->b_resid = resid;
55968a1ead9Stsutsui 		if (error != 0)
56068a1ead9Stsutsui 			bp->b_resid = bp->b_bcount;
56168a1ead9Stsutsui 
562a90704b5Stsutsui 		sc->is_busy = false;
56368a1ead9Stsutsui 		wakeup(&sc->is_busy);
56468a1ead9Stsutsui 
56568a1ead9Stsutsui 		s = splbio();
56668a1ead9Stsutsui 		disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
56768a1ead9Stsutsui 		    (bp->b_flags & B_READ) != 0);
56868a1ead9Stsutsui 		splx(s);
56968a1ead9Stsutsui 		biodone(bp);
57068a1ead9Stsutsui 	}
57168a1ead9Stsutsui 
57268a1ead9Stsutsui 	sc->is_active = false;
57368a1ead9Stsutsui }
57468a1ead9Stsutsui 
57536382f48Smarcus int
gdromioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)57653524e44Schristos gdromioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
57736382f48Smarcus {
5787df82ad1Smarcus 	struct gdrom_softc *sc;
5797df82ad1Smarcus 	int unit, error;
580f2d6cdb4Stsutsui 
581f2d6cdb4Stsutsui 	DPRINTF(("GDROM: ioctl %lx\n", cmd));
5827df82ad1Smarcus 
5837df82ad1Smarcus 	unit = DISKUNIT(dev);
584e6a3083cStsutsui 	sc = device_lookup_private(&gdrom_cd, unit);
5857df82ad1Smarcus 
5867df82ad1Smarcus 	switch (cmd) {
5877df82ad1Smarcus 	case CDIOREADMSADDR: {
5887d1d4a2fSmarcus 		int s, track, sessno = *(int *)addr;
5897df82ad1Smarcus 		struct gd_toc toc;
5907df82ad1Smarcus 
5917df82ad1Smarcus 		if (sessno != 0)
592cb6453dbStsutsui 			return EINVAL;
5937df82ad1Smarcus 
5947d1d4a2fSmarcus 		s = splbio();
5957d1d4a2fSmarcus 		while (sc->is_busy)
5967d1d4a2fSmarcus 			tsleep(&sc->is_busy, PRIBIO, "gdbusy", 0);
597a90704b5Stsutsui 		sc->is_busy = true;
5987d1d4a2fSmarcus 		splx(s);
5997d1d4a2fSmarcus 
6007d1d4a2fSmarcus 		error = gdrom_read_toc(sc, &toc);
6017d1d4a2fSmarcus 
602a90704b5Stsutsui 		sc->is_busy = false;
6037d1d4a2fSmarcus 		wakeup(&sc->is_busy);
6047d1d4a2fSmarcus 
605f2d6cdb4Stsutsui 		if (error != 0)
6067df82ad1Smarcus 			return error;
607d6cb28ddSabs #ifdef GDROMDEBUGTOC
608d6cb28ddSabs 		{ /* Dump the GDROM TOC */
609d6cb28ddSabs 		unsigned char *ptr = (unsigned char *)&toc;
610d6cb28ddSabs 		int i;
6117df82ad1Smarcus 
612d6cb28ddSabs 		printf("gdrom: TOC\n");
613d6cb28ddSabs 		for(i = 0; i < sizeof(toc); ++i) {
614d6cb28ddSabs 			printf("%02x", *ptr++);
615d6cb28ddSabs 			if( i%32 == 31)
616d6cb28ddSabs 				printf("\n");
617d6cb28ddSabs 			else if( i%4 == 3)
618d6cb28ddSabs 				printf(",");
619d6cb28ddSabs 		}
620d6cb28ddSabs 		printf("\n");
621d6cb28ddSabs 		}
622d6cb28ddSabs #endif
6237df82ad1Smarcus 		for (track = TOC_TRACK(toc.last);
6247df82ad1Smarcus 		    track >= TOC_TRACK(toc.first);
625d6cb28ddSabs 		    --track) {
626d6cb28ddSabs 			if (track < 1 || track > 100)
627d6cb28ddSabs 				return ENXIO;
6287df82ad1Smarcus 			if (TOC_CTRL(toc.entry[track - 1]))
6297df82ad1Smarcus 				break;
630d6cb28ddSabs 		}
6317df82ad1Smarcus 
632d6cb28ddSabs #ifdef GDROMDEBUGTOC
633d6cb28ddSabs 		printf("gdrom: Using track %d, LBA %u\n", track,
634d6cb28ddSabs 		    TOC_LBA(toc.entry[track - 1]));
635d6cb28ddSabs #endif
6367df82ad1Smarcus 
6377df82ad1Smarcus 		*(int *)addr = htonl(TOC_LBA(toc.entry[track - 1])) -
6387df82ad1Smarcus 		    sc->openpart_start;
6397df82ad1Smarcus 
640cb6453dbStsutsui 		return 0;
6417df82ad1Smarcus 	}
6427df82ad1Smarcus 	default:
64395919a0fStsutsui 		return ENOTTY;
6447df82ad1Smarcus 	}
6457df82ad1Smarcus 
6467df82ad1Smarcus #ifdef DIAGNOSTIC
6477df82ad1Smarcus 	panic("gdromioctl: impossible");
6487df82ad1Smarcus #endif
64936382f48Smarcus }
65036382f48Smarcus 
65136382f48Smarcus 
65236382f48Smarcus int
gdromread(dev_t dev,struct uio * uio,int flags)653b095a0d0Such gdromread(dev_t dev, struct uio *uio, int flags)
65436382f48Smarcus {
655f2d6cdb4Stsutsui 
656f2d6cdb4Stsutsui 	DPRINTF(("GDROM: read\n"));
657cb6453dbStsutsui 	return physio(gdromstrategy, NULL, dev, B_READ, minphys, uio);
65836382f48Smarcus }
65936382f48Smarcus 
66036382f48Smarcus int
gdromwrite(dev_t dev,struct uio * uio,int flags)661b095a0d0Such gdromwrite(dev_t dev, struct uio *uio, int flags)
66236382f48Smarcus {
663b095a0d0Such 
664cb6453dbStsutsui 	return EROFS;
66536382f48Smarcus }
666