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