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