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