1 /* $NetBSD: st_atapi.c,v 1.30 2015/08/24 23:13:15 pooka 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: st_atapi.c,v 1.30 2015/08/24 23:13:15 pooka Exp $"); 29 30 #ifdef _KERNEL_OPT 31 #include "opt_scsi.h" 32 #endif 33 34 #include <sys/param.h> 35 #include <sys/device.h> 36 #include <sys/buf.h> 37 #include <sys/bufq.h> 38 #include <sys/conf.h> 39 #include <sys/kernel.h> 40 #include <sys/systm.h> 41 42 #include <dev/scsipi/stvar.h> 43 #include <dev/scsipi/atapi_tape.h> 44 45 static int st_atapibus_match(device_t, cfdata_t, void *); 46 static void st_atapibus_attach(device_t, device_t, void *); 47 static int st_atapibus_ops(struct st_softc *, int, int); 48 static int st_atapibus_mode_sense(struct st_softc *, int); 49 50 CFATTACH_DECL_NEW( 51 st_atapibus, 52 sizeof(struct st_softc), 53 st_atapibus_match, 54 st_atapibus_attach, 55 stdetach, 56 NULL 57 ); 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(device_t parent, cfdata_t match, void *aux) 66 { 67 struct scsipibus_attach_args *sa = aux; 68 int priority; 69 70 if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph)) != 71 SCSIPI_BUSTYPE_ATAPI) 72 return 0; 73 74 (void)scsipi_inqmatch(&sa->sa_inqbuf, 75 st_atapibus_patterns, 76 sizeof(st_atapibus_patterns)/sizeof(st_atapibus_patterns[0]), 77 sizeof(st_atapibus_patterns[0]), &priority); 78 return priority; 79 } 80 81 static void 82 st_atapibus_attach(device_t parent, device_t self, void *aux) 83 { 84 struct st_softc *st = device_private(self); 85 struct scsipibus_attach_args *sa = aux; 86 struct scsipi_periph *periph = sa->sa_periph; 87 88 if (strcmp(sa->sa_inqbuf.vendor, "OnStream DI-30") == 0) { 89 struct ast_identifypage identify; 90 int error; 91 92 error = scsipi_mode_sense(periph, SMS_DBD, 93 ATAPI_TAPE_IDENTIFY_PAGE, &identify.header, 94 sizeof(identify), XS_CTL_DISCOVERY, 95 ST_RETRIES, ST_CTL_TIME); 96 if (error) { 97 printf("onstream get identify: error %d\n", error); 98 return; 99 } 100 strncpy(identify.ident, "NBSD", 4); 101 error = scsipi_mode_select(periph, SMS_PF, 102 &identify.header, sizeof(identify), 103 XS_CTL_DISCOVERY, 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, self, 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_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, 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