xref: /netbsd/sys/dev/scsipi/st_atapi.c (revision c4a72b64)
1 /*	$NetBSD: st_atapi.c,v 1.11 2002/10/02 16:52:56 thorpej 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.11 2002/10/02 16:52:56 thorpej 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 CFATTACH_DECL(st_atapibus, sizeof(struct st_softc),
58     st_atapibus_match, st_atapibus_attach, stdetach, stactivate);
59 
60 const struct scsipi_inquiry_pattern st_atapibus_patterns[] = {
61 	{T_SEQUENTIAL, T_REMOV,
62 	 "",	 "",		 ""},
63 };
64 
65 int
66 st_atapibus_match(parent, match, aux)
67 	struct device *parent;
68 	struct cfdata *match;
69 	void *aux;
70 {
71 	struct scsipibus_attach_args *sa = aux;
72 	int priority;
73 
74 	if (scsipi_periph_bustype(sa->sa_periph) != SCSIPI_BUSTYPE_ATAPI)
75 		return (0);
76 
77 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
78 	    (caddr_t)st_atapibus_patterns,
79 	    sizeof(st_atapibus_patterns)/sizeof(st_atapibus_patterns[0]),
80 	    sizeof(st_atapibus_patterns[0]), &priority);
81 	return (priority);
82 }
83 
84 void
85 st_atapibus_attach(parent, self, aux)
86 	struct device *parent, *self;
87 	void *aux;
88 {
89 	struct st_softc *st = (void *)self;
90 	struct scsipibus_attach_args *sa = aux;
91 	struct scsipi_periph *periph = sa->sa_periph;
92 
93 	if (strcmp(sa->sa_inqbuf.vendor, "OnStream DI-30") == 0) {
94 		struct ast_identifypage identify;
95 		int error;
96 
97 		error = scsipi_mode_sense(periph, SMS_DBD,
98 		    ATAPI_TAPE_IDENTIFY_PAGE, &identify.header,
99 		    sizeof(identify), XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK,
100 		    ST_RETRIES, ST_CTL_TIME);
101 		if (error) {
102 			printf("onstream get identify: error %d\n", error);
103 			return;
104 		}
105 		strncpy(identify.ident, "NBSD", 4);
106 		error = scsipi_mode_select(periph, SMS_PF,
107 		    &identify.header, sizeof(identify),
108 		    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK,
109 		    ST_RETRIES, ST_CTL_TIME);
110 		if (error) {
111 			printf("onstream set identify: error %d\n", error);
112 			return;
113 		}
114 	}
115 
116 	st->ops = st_atapibus_ops;
117 	stattach(parent, st, aux);
118 }
119 
120 int
121 st_atapibus_ops(st, op, flags)
122 	struct st_softc *st;
123 	int op;
124 	int flags;
125 {
126 	switch(op) {
127 	case ST_OPS_RBL:
128 		/* done in mode_sense */
129 		return 0;
130 	case ST_OPS_MODESENSE:
131 		return st_atapibus_mode_sense(st, flags);
132 	case ST_OPS_MODESELECT:
133 		return st_atapibus_mode_select(st, flags);
134 	case ST_OPS_CMPRSS_ON:
135 	case ST_OPS_CMPRSS_OFF:
136 		return ENODEV;
137 	default:
138 		panic("st_scsibus_ops: invalid op");
139 		/* NOTREACHED */
140 	}
141 }
142 
143 int
144 st_atapibus_mode_sense(st, flags)
145 	struct st_softc *st;
146 	int flags;
147 {
148 	int count, error;
149 	struct atapi_cappage cappage;
150 	struct scsipi_periph *periph = st->sc_periph;
151 
152 	/* get drive capabilities, some drives needs this repeated */
153 	for (count = 0 ; count < 5 ; count++) {
154 		error = scsipi_mode_sense(periph, SMS_DBD,
155 		    ATAPI_TAPE_CAP_PAGE, &cappage.header, sizeof(cappage),
156 		    flags | XS_CTL_DATA_ONSTACK, ST_RETRIES, ST_CTL_TIME);
157 		if (error == 0) {
158 			st->numblks = 0; /* unused anyway */
159 			if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK32K)
160 				st->media_blksize = 32768;
161 			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK1K)
162 				st->media_blksize = 1024;
163 			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK512)
164 				st->media_blksize = 512;
165 			else {
166 				error = ENODEV;
167 				continue;
168 			}
169 			st->blkmin = st->blkmax = st->media_blksize;
170 			st->media_density = 0;
171 			if (cappage.header.dev_spec & SMH_DSP_WRITE_PROT)
172 				st->flags |= ST_READONLY;
173 			else
174 				st->flags &= ~ST_READONLY;
175 			SC_DEBUG(periph, SCSIPI_DB3,
176 			    ("density code %d, %d-byte blocks, write-%s, ",
177 			    st->media_density, st->media_blksize,
178 			    st->flags & ST_READONLY ? "protected" : "enabled"));
179 			SC_DEBUG(periph, SCSIPI_DB3,
180 			    ("%sbuffered\n",
181 			    cappage.header.dev_spec & SMH_DSP_BUFF_MODE ?
182 			    "" : "un"));
183 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
184 			return 0;
185 		}
186 	}
187 	return error;
188 }
189 
190 int
191 st_atapibus_mode_select(st, flags)
192 	struct st_softc *st;
193 	int flags;
194 {
195 	return ENODEV; /* for now ... */
196 }
197