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