xref: /netbsd-src/sys/dev/scsipi/st_atapi.c (revision 8e6f7afb5bba2c07fd084c08249718961dfbc1d1)
1 /*	$NetBSD: st_atapi.c,v 1.7 2001/12/07 11:26:30 yamt Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: st_atapi.c,v 1.7 2001/12/07 11:26:30 yamt Exp $");
37 
38 #include "opt_scsi.h"
39 #include "rnd.h"
40 
41 #include <sys/param.h>
42 #include <sys/device.h>
43 #include <sys/buf.h>
44 #include <sys/conf.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 
48 #include <dev/scsipi/stvar.h>
49 #include <dev/scsipi/atapi_tape.h>
50 
51 int	st_atapibus_match __P((struct device *, struct cfdata *, void *));
52 void	st_atapibus_attach __P((struct device *, struct device *, void *));
53 int	st_atapibus_ops __P((struct st_softc *, int, int));
54 int	st_atapibus_mode_sense __P((struct st_softc *, int));
55 int	st_atapibus_mode_select __P((struct st_softc *, int));
56 int	st_atapibus_do_ms __P((struct st_softc *, int, void *, int, int));
57 
58 struct cfattach st_atapibus_ca = {
59 	sizeof(struct st_softc), st_atapibus_match, st_atapibus_attach,
60 	stdetach, stactivate
61 };
62 
63 const struct scsipi_inquiry_pattern st_atapibus_patterns[] = {
64 	{T_SEQUENTIAL, T_REMOV,
65 	 "",	 "",		 ""},
66 };
67 
68 int
69 st_atapibus_match(parent, match, aux)
70 	struct device *parent;
71 	struct cfdata *match;
72 	void *aux;
73 {
74 	struct scsipibus_attach_args *sa = aux;
75 	int priority;
76 
77 	if (scsipi_periph_bustype(sa->sa_periph) != SCSIPI_BUSTYPE_ATAPI)
78 		return (0);
79 
80 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
81 	    (caddr_t)st_atapibus_patterns,
82 	    sizeof(st_atapibus_patterns)/sizeof(st_atapibus_patterns[0]),
83 	    sizeof(st_atapibus_patterns[0]), &priority);
84 	return (priority);
85 }
86 
87 void
88 st_atapibus_attach(parent, self, aux)
89 	struct device *parent, *self;
90 	void *aux;
91 {
92 	struct st_softc *st = (void *)self;
93 	struct scsipibus_attach_args *sa = aux;
94 	struct scsipi_periph *periph = sa->sa_periph;
95 
96 	if (strcmp(sa->sa_inqbuf.vendor, "OnStream DI-30") == 0) {
97 		struct ast_identifypage identify;
98 		int error;
99 
100 		error = scsipi_mode_sense(periph, SMS_DBD,
101 		    ATAPI_TAPE_IDENTIFY_PAGE, &identify.header,
102 		    sizeof(identify), XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK,
103 		    ST_RETRIES, ST_CTL_TIME);
104 		if (error) {
105 			printf("onstream get identify: error %d\n", error);
106 			return;
107 		}
108 		strncpy(identify.ident, "NBSD", 4);
109 		error = scsipi_mode_select(periph, SMS_PF,
110 		    &identify.header, sizeof(identify),
111 		    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK,
112 		    ST_RETRIES, ST_CTL_TIME);
113 		if (error) {
114 			printf("onstream set identify: error %d\n", error);
115 			return;
116 		}
117 	}
118 
119 	st->ops = st_atapibus_ops;
120 	stattach(parent, st, aux);
121 }
122 
123 int
124 st_atapibus_ops(st, op, flags)
125 	struct st_softc *st;
126 	int op;
127 	int flags;
128 {
129 	switch(op) {
130 	case ST_OPS_RBL:
131 		/* done in mode_sense */
132 		return 0;
133 	case ST_OPS_MODESENSE:
134 		return st_atapibus_mode_sense(st, flags);
135 	case ST_OPS_MODESELECT:
136 		return st_atapibus_mode_select(st, flags);
137 	case ST_OPS_CMPRSS_ON:
138 	case ST_OPS_CMPRSS_OFF:
139 		return ENODEV;
140 	default:
141 		panic("st_scsibus_ops: invalid op");
142 		/* NOTREACHED */
143 	}
144 }
145 
146 int
147 st_atapibus_mode_sense(st, flags)
148 	struct st_softc *st;
149 	int flags;
150 {
151 	int count, error;
152 	struct atapi_cappage cappage;
153 	struct scsipi_periph *periph = st->sc_periph;
154 
155 	/* get drive capabilities, some drives needs this repeated */
156 	for (count = 0 ; count < 5 ; count++) {
157 		error = scsipi_mode_sense(periph, SMS_DBD,
158 		    ATAPI_TAPE_CAP_PAGE, &cappage.header, sizeof(cappage),
159 		    flags | XS_CTL_DATA_ONSTACK, ST_RETRIES, ST_CTL_TIME);
160 		if (error == 0) {
161 			st->numblks = 0; /* unused anyway */
162 			if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK32K)
163 				st->media_blksize = 32768;
164 			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK1K)
165 				st->media_blksize = 1024;
166 			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK512)
167 				st->media_blksize = 512;
168 			else {
169 				error = ENODEV;
170 				continue;
171 			}
172 			st->blkmin = st->blkmax = st->media_blksize;
173 			st->media_density = 0;
174 			if (cappage.header.dev_spec & SMH_DSP_WRITE_PROT)
175 				st->flags |= ST_READONLY;
176 			else
177 				st->flags &= ~ST_READONLY;
178 			SC_DEBUG(periph, SCSIPI_DB3,
179 			    ("density code %d, %d-byte blocks, write-%s, ",
180 			    st->media_density, st->media_blksize,
181 			    st->flags & ST_READONLY ? "protected" : "enabled"));
182 			SC_DEBUG(periph, SCSIPI_DB3,
183 			    ("%sbuffered\n",
184 			    cappage.header.dev_spec & SMH_DSP_BUFF_MODE ?
185 			    "" : "un"));
186 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
187 			return 0;
188 		}
189 	}
190 	return error;
191 }
192 
193 int
194 st_atapibus_mode_select(st, flags)
195 	struct st_softc *st;
196 	int flags;
197 {
198 	return ENODEV; /* for now ... */
199 }
200