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