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