1/*	$NetBSD: st_atapi.c,v 1.27.2.1 2012/03/02 16:33:40 riz 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.27.2.1 2012/03/02 16:33:40 riz 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
43static int	st_atapibus_match(device_t, cfdata_t, void *);
44static void	st_atapibus_attach(device_t, device_t, void *);
45static int	st_atapibus_ops(struct st_softc *, int, int);
46static int	st_atapibus_mode_sense(struct st_softc *, int);
47
48CFATTACH_DECL_NEW(
49	st_atapibus,
50	sizeof(struct st_softc),
51	st_atapibus_match,
52	st_atapibus_attach,
53	stdetach,
54	NULL
55);
56
57static const struct scsipi_inquiry_pattern st_atapibus_patterns[] = {
58	{T_SEQUENTIAL, T_REMOV,
59	 "",	 "",		 ""},
60};
61
62static int
63st_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_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph)) !=
69	    SCSIPI_BUSTYPE_ATAPI)
70		return 0;
71
72	(void)scsipi_inqmatch(&sa->sa_inqbuf,
73	    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
79static void
80st_atapibus_attach(device_t parent, device_t self, void *aux)
81{
82	struct st_softc *st = device_private(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,
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, ST_RETRIES, ST_CTL_TIME);
102		if (error) {
103			printf("onstream set identify: error %d\n", error);
104			return;
105		}
106	}
107
108	st->ops = st_atapibus_ops;
109	stattach(parent, self, aux);
110}
111
112static int
113st_atapibus_ops(struct st_softc *st, int op, int flags)
114{
115	switch(op) {
116	case ST_OPS_RBL:
117		/* done in mode_sense */
118		return 0;
119	case ST_OPS_MODESENSE:
120		return st_atapibus_mode_sense(st, flags);
121	case ST_OPS_MODESELECT:
122		return st_mode_select(st, flags);
123	case ST_OPS_CMPRSS_ON:
124	case ST_OPS_CMPRSS_OFF:
125		return ENODEV;
126	default:
127		panic("st_scsibus_ops: invalid op");
128		/* NOTREACHED */
129	}
130}
131
132static int
133st_atapibus_mode_sense(struct st_softc *st, int flags)
134{
135	int count, error;
136	struct atapi_cappage cappage;
137	struct scsipi_periph *periph = st->sc_periph;
138
139	/* get drive capabilities, some drives needs this repeated */
140	for (count = 0 ; count < 5 ; count++) {
141		error = scsipi_mode_sense(periph, SMS_DBD,
142		    ATAPI_TAPE_CAP_PAGE, &cappage.header, sizeof(cappage),
143		    flags, ST_RETRIES, ST_CTL_TIME);
144		if (error == 0) {
145			st->numblks = 0; /* unused anyway */
146			if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK32K)
147				st->media_blksize = 32768;
148			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK1K)
149				st->media_blksize = 1024;
150			else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK512)
151				st->media_blksize = 512;
152			else {
153				error = ENODEV;
154				continue;
155			}
156			st->blkmin = st->blkmax = st->media_blksize;
157			st->media_density = 0;
158			if (cappage.header.dev_spec & SMH_DSP_WRITE_PROT)
159				st->flags |= ST_READONLY;
160			else
161				st->flags &= ~ST_READONLY;
162			SC_DEBUG(periph, SCSIPI_DB3,
163			    ("density code %d, %d-byte blocks, write-%s, ",
164			    st->media_density, st->media_blksize,
165			    st->flags & ST_READONLY ? "protected" : "enabled"));
166			SC_DEBUG(periph, SCSIPI_DB3,
167			    ("%sbuffered\n",
168			    cappage.header.dev_spec & SMH_DSP_BUFF_MODE ?
169			    "" : "un"));
170			periph->periph_flags |= PERIPH_MEDIA_LOADED;
171			return 0;
172		}
173	}
174	return error;
175}
176