umass_scsipi.c revision 1.4.4.2 1 1.4.4.2 thorpej /* $NetBSD: umass_scsipi.c,v 1.4.4.2 2002/01/10 19:59:01 thorpej Exp $ */
2 1.4.4.2 thorpej
3 1.4.4.2 thorpej /*
4 1.4.4.2 thorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.4.4.2 thorpej * All rights reserved.
6 1.4.4.2 thorpej *
7 1.4.4.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.4.4.2 thorpej * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.4.4.2 thorpej * Carlstedt Research & Technology.
10 1.4.4.2 thorpej *
11 1.4.4.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.4.4.2 thorpej * modification, are permitted provided that the following conditions
13 1.4.4.2 thorpej * are met:
14 1.4.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.4.4.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.4.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.4.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.4.4.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.4.4.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.4.4.2 thorpej * must display the following acknowledgement:
21 1.4.4.2 thorpej * This product includes software developed by the NetBSD
22 1.4.4.2 thorpej * Foundation, Inc. and its contributors.
23 1.4.4.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.4.4.2 thorpej * contributors may be used to endorse or promote products derived
25 1.4.4.2 thorpej * from this software without specific prior written permission.
26 1.4.4.2 thorpej *
27 1.4.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.4.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.4.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.4.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.4.4.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.4.4.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.4.4.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.4.4.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.4.4.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.4.4.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.4.4.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.4.4.2 thorpej */
39 1.4.4.2 thorpej
40 1.4.4.2 thorpej #include <sys/cdefs.h>
41 1.4.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.4.4.2 2002/01/10 19:59:01 thorpej Exp $");
42 1.4.4.2 thorpej
43 1.4.4.2 thorpej #include "atapibus.h"
44 1.4.4.2 thorpej #include "scsibus.h"
45 1.4.4.2 thorpej
46 1.4.4.2 thorpej #include <sys/param.h>
47 1.4.4.2 thorpej #include <sys/systm.h>
48 1.4.4.2 thorpej #include <sys/kernel.h>
49 1.4.4.2 thorpej #include <sys/conf.h>
50 1.4.4.2 thorpej #include <sys/buf.h>
51 1.4.4.2 thorpej #include <sys/device.h>
52 1.4.4.2 thorpej #include <sys/ioctl.h>
53 1.4.4.2 thorpej #include <sys/malloc.h>
54 1.4.4.2 thorpej
55 1.4.4.2 thorpej /* SCSI & ATAPI */
56 1.4.4.2 thorpej #include <sys/scsiio.h>
57 1.4.4.2 thorpej #include <dev/scsipi/scsi_all.h>
58 1.4.4.2 thorpej #include <dev/scsipi/scsipi_all.h>
59 1.4.4.2 thorpej #include <dev/scsipi/scsiconf.h>
60 1.4.4.2 thorpej
61 1.4.4.2 thorpej #include <dev/scsipi/atapiconf.h>
62 1.4.4.2 thorpej
63 1.4.4.2 thorpej #include <dev/scsipi/scsipi_disk.h>
64 1.4.4.2 thorpej #include <dev/scsipi/scsi_disk.h>
65 1.4.4.2 thorpej #include <dev/scsipi/scsi_changer.h>
66 1.4.4.2 thorpej
67 1.4.4.2 thorpej #include <dev/scsipi/atapi_disk.h>
68 1.4.4.2 thorpej
69 1.4.4.2 thorpej #include <sys/disk.h> /* XXX */
70 1.4.4.2 thorpej #include <dev/scsipi/sdvar.h> /* XXX */
71 1.4.4.2 thorpej
72 1.4.4.2 thorpej /* USB */
73 1.4.4.2 thorpej #include <dev/usb/usb.h>
74 1.4.4.2 thorpej #include <dev/usb/usbdi.h>
75 1.4.4.2 thorpej #include <dev/usb/usbdi_util.h>
76 1.4.4.2 thorpej #include <dev/usb/usbdevs.h>
77 1.4.4.2 thorpej
78 1.4.4.2 thorpej #include <dev/usb/umassvar.h>
79 1.4.4.2 thorpej #include <dev/usb/umass_scsipi.h>
80 1.4.4.2 thorpej
81 1.4.4.2 thorpej struct umass_scsipi_softc {
82 1.4.4.2 thorpej struct umassbus_softc base;
83 1.4.4.2 thorpej
84 1.4.4.2 thorpej struct atapi_adapter sc_atapi_adapter;
85 1.4.4.2 thorpej #define sc_adapter sc_atapi_adapter._generic
86 1.4.4.2 thorpej struct scsipi_channel sc_channel;
87 1.4.4.2 thorpej usbd_status sc_sync_status;
88 1.4.4.2 thorpej struct scsipi_sense sc_sense_cmd;
89 1.4.4.2 thorpej };
90 1.4.4.2 thorpej
91 1.4.4.2 thorpej
92 1.4.4.2 thorpej #define SHORT_INQUIRY_LENGTH 36 /* XXX */
93 1.4.4.2 thorpej
94 1.4.4.2 thorpej #define UMASS_SCSIID_HOST 0x00
95 1.4.4.2 thorpej #define UMASS_SCSIID_DEVICE 0x01
96 1.4.4.2 thorpej
97 1.4.4.2 thorpej #define UMASS_ATAPI_DRIVE 0
98 1.4.4.2 thorpej
99 1.4.4.2 thorpej Static void umass_scsipi_request(struct scsipi_channel *,
100 1.4.4.2 thorpej scsipi_adapter_req_t, void *);
101 1.4.4.2 thorpej Static void umass_scsipi_minphys(struct buf *bp);
102 1.4.4.2 thorpej Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
103 1.4.4.2 thorpej caddr_t, int, usb_proc_ptr );
104 1.4.4.2 thorpej Static int umass_scsipi_getgeom(struct scsipi_periph *periph,
105 1.4.4.2 thorpej struct disk_parms *, u_long sectors);
106 1.4.4.2 thorpej
107 1.4.4.2 thorpej Static void umass_scsipi_cb(struct umass_softc *sc, void *priv,
108 1.4.4.2 thorpej int residue, int status);
109 1.4.4.2 thorpej Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv,
110 1.4.4.2 thorpej int residue, int status);
111 1.4.4.2 thorpej
112 1.4.4.2 thorpej Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *sc);
113 1.4.4.2 thorpej
114 1.4.4.2 thorpej Static int scsipiprint(void *aux, const char *pnp);
115 1.4.4.2 thorpej
116 1.4.4.2 thorpej #if NATAPIBUS > 0
117 1.4.4.2 thorpej Static void umass_atapi_probe_device(struct atapibus_softc *, int);
118 1.4.4.2 thorpej
119 1.4.4.2 thorpej const struct scsipi_bustype umass_atapi_bustype = {
120 1.4.4.2 thorpej SCSIPI_BUSTYPE_ATAPI,
121 1.4.4.2 thorpej atapi_scsipi_cmd,
122 1.4.4.2 thorpej atapi_interpret_sense,
123 1.4.4.2 thorpej atapi_print_addr,
124 1.4.4.2 thorpej scsi_kill_pending,
125 1.4.4.2 thorpej };
126 1.4.4.2 thorpej #endif
127 1.4.4.2 thorpej
128 1.4.4.2 thorpej
129 1.4.4.2 thorpej #if NSCSIBUS > 0
130 1.4.4.2 thorpej int
131 1.4.4.2 thorpej umass_scsi_attach(struct umass_softc *sc)
132 1.4.4.2 thorpej {
133 1.4.4.2 thorpej struct umass_scsipi_softc *scbus;
134 1.4.4.2 thorpej
135 1.4.4.2 thorpej scbus = umass_scsipi_setup(sc);
136 1.4.4.2 thorpej
137 1.4.4.2 thorpej scbus->sc_channel.chan_bustype = &scsi_bustype;
138 1.4.4.2 thorpej scbus->sc_channel.chan_ntargets = UMASS_SCSIID_DEVICE + 1;
139 1.4.4.2 thorpej scbus->sc_channel.chan_nluns = sc->maxlun + 1;
140 1.4.4.2 thorpej scbus->sc_channel.chan_id = UMASS_SCSIID_HOST;
141 1.4.4.2 thorpej DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n",
142 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev)));
143 1.4.4.2 thorpej scbus->base.sc_child =
144 1.4.4.2 thorpej config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
145 1.4.4.2 thorpej
146 1.4.4.2 thorpej return (0);
147 1.4.4.2 thorpej }
148 1.4.4.2 thorpej #endif
149 1.4.4.2 thorpej
150 1.4.4.2 thorpej #if NATAPIBUS > 0
151 1.4.4.2 thorpej int
152 1.4.4.2 thorpej umass_atapi_attach(struct umass_softc *sc)
153 1.4.4.2 thorpej {
154 1.4.4.2 thorpej struct umass_scsipi_softc *scbus;
155 1.4.4.2 thorpej
156 1.4.4.2 thorpej scbus = umass_scsipi_setup(sc);
157 1.4.4.2 thorpej scbus->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device;
158 1.4.4.2 thorpej
159 1.4.4.2 thorpej scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
160 1.4.4.2 thorpej scbus->sc_channel.chan_ntargets = 2;
161 1.4.4.2 thorpej scbus->sc_channel.chan_nluns = 1;
162 1.4.4.2 thorpej
163 1.4.4.2 thorpej scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
164 1.4.4.2 thorpej DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n",
165 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev)));
166 1.4.4.2 thorpej scbus->base.sc_child =
167 1.4.4.2 thorpej config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
168 1.4.4.2 thorpej
169 1.4.4.2 thorpej return (0);
170 1.4.4.2 thorpej }
171 1.4.4.2 thorpej #endif
172 1.4.4.2 thorpej
173 1.4.4.2 thorpej Static struct umass_scsipi_softc *
174 1.4.4.2 thorpej umass_scsipi_setup(struct umass_softc *sc)
175 1.4.4.2 thorpej {
176 1.4.4.2 thorpej struct umass_scsipi_softc *scbus;
177 1.4.4.2 thorpej
178 1.4.4.2 thorpej scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO);
179 1.4.4.2 thorpej sc->bus = &scbus->base;
180 1.4.4.2 thorpej
181 1.4.4.2 thorpej /* Only use big commands for USB SCSI devices. */
182 1.4.4.2 thorpej sc->sc_busquirks |= PQUIRK_ONLYBIG;
183 1.4.4.2 thorpej
184 1.4.4.2 thorpej /* Fill in the adapter. */
185 1.4.4.2 thorpej memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
186 1.4.4.2 thorpej scbus->sc_adapter.adapt_dev = &sc->sc_dev;
187 1.4.4.2 thorpej scbus->sc_adapter.adapt_nchannels = 1;
188 1.4.4.2 thorpej scbus->sc_adapter.adapt_request = umass_scsipi_request;
189 1.4.4.2 thorpej scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
190 1.4.4.2 thorpej scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
191 1.4.4.2 thorpej scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
192 1.4.4.2 thorpej
193 1.4.4.2 thorpej /* Fill in the channel. */
194 1.4.4.2 thorpej memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
195 1.4.4.2 thorpej scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
196 1.4.4.2 thorpej scbus->sc_channel.chan_channel = 0;
197 1.4.4.2 thorpej scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS;
198 1.4.4.2 thorpej scbus->sc_channel.chan_openings = 1;
199 1.4.4.2 thorpej scbus->sc_channel.chan_max_periph = 1;
200 1.4.4.2 thorpej scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
201 1.4.4.2 thorpej
202 1.4.4.2 thorpej return (scbus);
203 1.4.4.2 thorpej }
204 1.4.4.2 thorpej
205 1.4.4.2 thorpej Static int
206 1.4.4.2 thorpej scsipiprint(void *aux, const char *pnp)
207 1.4.4.2 thorpej {
208 1.4.4.2 thorpej struct scsipi_channel *chan = aux;
209 1.4.4.2 thorpej
210 1.4.4.2 thorpej if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_SCSI) {
211 1.4.4.2 thorpej #if NSCSIBUS > 0
212 1.4.4.2 thorpej return (scsiprint(aux, pnp));
213 1.4.4.2 thorpej #else
214 1.4.4.2 thorpej if (pnp)
215 1.4.4.2 thorpej printf("scsibus at %s", pnp);
216 1.4.4.2 thorpej return (UNCONF);
217 1.4.4.2 thorpej #endif
218 1.4.4.2 thorpej } else {
219 1.4.4.2 thorpej #if NATAPIBUS > 0
220 1.4.4.2 thorpej return (atapiprint(aux, pnp));
221 1.4.4.2 thorpej #else
222 1.4.4.2 thorpej if (pnp)
223 1.4.4.2 thorpej printf("atapibus at %s", pnp);
224 1.4.4.2 thorpej return (UNCONF);
225 1.4.4.2 thorpej #endif
226 1.4.4.2 thorpej }
227 1.4.4.2 thorpej }
228 1.4.4.2 thorpej
229 1.4.4.2 thorpej Static void
230 1.4.4.2 thorpej umass_scsipi_request(struct scsipi_channel *chan,
231 1.4.4.2 thorpej scsipi_adapter_req_t req, void *arg)
232 1.4.4.2 thorpej {
233 1.4.4.2 thorpej struct scsipi_adapter *adapt = chan->chan_adapter;
234 1.4.4.2 thorpej struct scsipi_periph *periph;
235 1.4.4.2 thorpej struct scsipi_xfer *xs;
236 1.4.4.2 thorpej struct umass_softc *sc = (void *)adapt->adapt_dev;
237 1.4.4.2 thorpej struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
238 1.4.4.2 thorpej struct scsipi_generic *cmd, trcmd;
239 1.4.4.2 thorpej int cmdlen;
240 1.4.4.2 thorpej int dir;
241 1.4.4.2 thorpej #ifdef UMASS_DEBUG
242 1.4.4.2 thorpej microtime(&sc->tv);
243 1.4.4.2 thorpej #endif
244 1.4.4.2 thorpej switch(req) {
245 1.4.4.2 thorpej case ADAPTER_REQ_RUN_XFER:
246 1.4.4.2 thorpej xs = arg;
247 1.4.4.2 thorpej periph = xs->xs_periph;
248 1.4.4.2 thorpej DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
249 1.4.4.2 thorpej
250 1.4.4.2 thorpej DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %lu.%06lu: %d:%d "
251 1.4.4.2 thorpej "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
252 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev), sc->tv.tv_sec, sc->tv.tv_usec,
253 1.4.4.2 thorpej periph->periph_target, periph->periph_lun,
254 1.4.4.2 thorpej xs, xs->cmd->opcode, xs->datalen,
255 1.4.4.2 thorpej periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
256 1.4.4.2 thorpej #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG)
257 1.4.4.2 thorpej if (umassdebug & UDMASS_SCSI)
258 1.4.4.2 thorpej show_scsipi_xs(xs);
259 1.4.4.2 thorpej else if (umassdebug & ~UDMASS_CMD)
260 1.4.4.2 thorpej show_scsipi_cmd(xs);
261 1.4.4.2 thorpej #endif
262 1.4.4.2 thorpej
263 1.4.4.2 thorpej if (sc->sc_dying) {
264 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
265 1.4.4.2 thorpej goto done;
266 1.4.4.2 thorpej }
267 1.4.4.2 thorpej
268 1.4.4.2 thorpej #ifdef UMASS_DEBUG
269 1.4.4.2 thorpej if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_ATAPI ?
270 1.4.4.2 thorpej periph->periph_target != UMASS_ATAPI_DRIVE :
271 1.4.4.2 thorpej periph->periph_target != UMASS_SCSIID_DEVICE) {
272 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
273 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev),
274 1.4.4.2 thorpej periph->periph_target));
275 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
276 1.4.4.2 thorpej goto done;
277 1.4.4.2 thorpej }
278 1.4.4.2 thorpej #endif
279 1.4.4.2 thorpej
280 1.4.4.2 thorpej cmd = xs->cmd;
281 1.4.4.2 thorpej cmdlen = xs->cmdlen;
282 1.4.4.2 thorpej
283 1.4.4.2 thorpej /* XXX should use transform */
284 1.4.4.2 thorpej
285 1.4.4.2 thorpej if (cmd->opcode == START_STOP &&
286 1.4.4.2 thorpej (sc->sc_quirks & UMASS_QUIRK_NO_START_STOP)) {
287 1.4.4.2 thorpej /*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
288 1.4.4.2 thorpej xs->error = XS_NOERROR;
289 1.4.4.2 thorpej goto done;
290 1.4.4.2 thorpej }
291 1.4.4.2 thorpej
292 1.4.4.2 thorpej if (cmd->opcode == INQUIRY &&
293 1.4.4.2 thorpej (sc->sc_quirks & UMASS_QUIRK_FORCE_SHORT_INQUIRY)) {
294 1.4.4.2 thorpej /*
295 1.4.4.2 thorpej * some drives wedge when asked for full inquiry
296 1.4.4.2 thorpej * information.
297 1.4.4.2 thorpej */
298 1.4.4.2 thorpej memcpy(&trcmd, cmd, sizeof trcmd);
299 1.4.4.2 thorpej trcmd.bytes[4] = SHORT_INQUIRY_LENGTH;
300 1.4.4.2 thorpej cmd = &trcmd;
301 1.4.4.2 thorpej }
302 1.4.4.2 thorpej
303 1.4.4.2 thorpej dir = DIR_NONE;
304 1.4.4.2 thorpej if (xs->datalen) {
305 1.4.4.2 thorpej switch (xs->xs_control &
306 1.4.4.2 thorpej (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
307 1.4.4.2 thorpej case XS_CTL_DATA_IN:
308 1.4.4.2 thorpej dir = DIR_IN;
309 1.4.4.2 thorpej break;
310 1.4.4.2 thorpej case XS_CTL_DATA_OUT:
311 1.4.4.2 thorpej dir = DIR_OUT;
312 1.4.4.2 thorpej break;
313 1.4.4.2 thorpej }
314 1.4.4.2 thorpej }
315 1.4.4.2 thorpej
316 1.4.4.2 thorpej if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
317 1.4.4.2 thorpej printf("umass_cmd: large datalen, %d\n", xs->datalen);
318 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
319 1.4.4.2 thorpej goto done;
320 1.4.4.2 thorpej }
321 1.4.4.2 thorpej
322 1.4.4.2 thorpej if (xs->xs_control & XS_CTL_POLL) {
323 1.4.4.2 thorpej /* Use sync transfer. XXX Broken! */
324 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI,
325 1.4.4.2 thorpej ("umass_scsi_cmd: sync dir=%d\n", dir));
326 1.4.4.2 thorpej sc->sc_xfer_flags = USBD_SYNCHRONOUS;
327 1.4.4.2 thorpej scbus->sc_sync_status = USBD_INVAL;
328 1.4.4.2 thorpej sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
329 1.4.4.2 thorpej cmdlen, xs->data,
330 1.4.4.2 thorpej xs->datalen, dir,
331 1.4.4.2 thorpej xs->timeout, 0, xs);
332 1.4.4.2 thorpej sc->sc_xfer_flags = 0;
333 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
334 1.4.4.2 thorpej scbus->sc_sync_status));
335 1.4.4.2 thorpej switch (scbus->sc_sync_status) {
336 1.4.4.2 thorpej case USBD_NORMAL_COMPLETION:
337 1.4.4.2 thorpej xs->error = XS_NOERROR;
338 1.4.4.2 thorpej break;
339 1.4.4.2 thorpej case USBD_TIMEOUT:
340 1.4.4.2 thorpej xs->error = XS_TIMEOUT;
341 1.4.4.2 thorpej break;
342 1.4.4.2 thorpej default:
343 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
344 1.4.4.2 thorpej break;
345 1.4.4.2 thorpej }
346 1.4.4.2 thorpej goto done;
347 1.4.4.2 thorpej } else {
348 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI,
349 1.4.4.2 thorpej ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
350 1.4.4.2 thorpej " datalen=%d\n",
351 1.4.4.2 thorpej dir, cmdlen, xs->datalen));
352 1.4.4.2 thorpej sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
353 1.4.4.2 thorpej cmdlen, xs->data,
354 1.4.4.2 thorpej xs->datalen, dir,
355 1.4.4.2 thorpej xs->timeout,
356 1.4.4.2 thorpej umass_scsipi_cb, xs);
357 1.4.4.2 thorpej return;
358 1.4.4.2 thorpej }
359 1.4.4.2 thorpej
360 1.4.4.2 thorpej /* Return if command finishes early. */
361 1.4.4.2 thorpej done:
362 1.4.4.2 thorpej scsipi_done(xs);
363 1.4.4.2 thorpej return;
364 1.4.4.2 thorpej default:
365 1.4.4.2 thorpej /* Not supported, nothing to do. */
366 1.4.4.2 thorpej ;
367 1.4.4.2 thorpej }
368 1.4.4.2 thorpej }
369 1.4.4.2 thorpej
370 1.4.4.2 thorpej Static void
371 1.4.4.2 thorpej umass_scsipi_minphys(struct buf *bp)
372 1.4.4.2 thorpej {
373 1.4.4.2 thorpej #ifdef DIAGNOSTIC
374 1.4.4.2 thorpej if (bp->b_bcount <= 0) {
375 1.4.4.2 thorpej printf("umass_scsipi_minphys count(%ld) <= 0\n",
376 1.4.4.2 thorpej bp->b_bcount);
377 1.4.4.2 thorpej bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
378 1.4.4.2 thorpej }
379 1.4.4.2 thorpej #endif
380 1.4.4.2 thorpej if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
381 1.4.4.2 thorpej bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
382 1.4.4.2 thorpej minphys(bp);
383 1.4.4.2 thorpej }
384 1.4.4.2 thorpej
385 1.4.4.2 thorpej int
386 1.4.4.2 thorpej umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t arg,
387 1.4.4.2 thorpej int flag, usb_proc_ptr p)
388 1.4.4.2 thorpej {
389 1.4.4.2 thorpej /*struct umass_softc *sc = link->adapter_softc;*/
390 1.4.4.2 thorpej /*struct umass_scsipi_softc *scbus = sc->bus;*/
391 1.4.4.2 thorpej
392 1.4.4.2 thorpej switch (cmd) {
393 1.4.4.2 thorpej #if 0
394 1.4.4.2 thorpej case SCBUSIORESET:
395 1.4.4.2 thorpej ccb->ccb_h.status = CAM_REQ_INPROG;
396 1.4.4.2 thorpej umass_reset(sc, umass_cam_cb, (void *) ccb);
397 1.4.4.2 thorpej return (0);
398 1.4.4.2 thorpej #endif
399 1.4.4.2 thorpej default:
400 1.4.4.2 thorpej return (ENOTTY);
401 1.4.4.2 thorpej }
402 1.4.4.2 thorpej }
403 1.4.4.2 thorpej
404 1.4.4.2 thorpej Static int
405 1.4.4.2 thorpej umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
406 1.4.4.2 thorpej u_long sectors)
407 1.4.4.2 thorpej {
408 1.4.4.2 thorpej struct umass_softc *sc =
409 1.4.4.2 thorpej (void *)periph->periph_channel->chan_adapter->adapt_dev;
410 1.4.4.2 thorpej
411 1.4.4.2 thorpej /* If it's not a floppy, we don't know what to do. */
412 1.4.4.2 thorpej if (sc->sc_cmd != UMASS_CPROTO_UFI)
413 1.4.4.2 thorpej return (0);
414 1.4.4.2 thorpej
415 1.4.4.2 thorpej switch (sectors) {
416 1.4.4.2 thorpej case 1440:
417 1.4.4.2 thorpej /* Most likely a single density 3.5" floppy. */
418 1.4.4.2 thorpej dp->heads = 2;
419 1.4.4.2 thorpej dp->sectors = 9;
420 1.4.4.2 thorpej dp->cyls = 80;
421 1.4.4.2 thorpej return (1);
422 1.4.4.2 thorpej case 2880:
423 1.4.4.2 thorpej /* Most likely a double density 3.5" floppy. */
424 1.4.4.2 thorpej dp->heads = 2;
425 1.4.4.2 thorpej dp->sectors = 18;
426 1.4.4.2 thorpej dp->cyls = 80;
427 1.4.4.2 thorpej return (1);
428 1.4.4.2 thorpej default:
429 1.4.4.2 thorpej return (0);
430 1.4.4.2 thorpej }
431 1.4.4.2 thorpej }
432 1.4.4.2 thorpej
433 1.4.4.2 thorpej Static void
434 1.4.4.2 thorpej umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
435 1.4.4.2 thorpej {
436 1.4.4.2 thorpej struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
437 1.4.4.2 thorpej struct scsipi_xfer *xs = priv;
438 1.4.4.2 thorpej struct scsipi_periph *periph = xs->xs_periph;
439 1.4.4.2 thorpej int cmdlen;
440 1.4.4.2 thorpej int s;
441 1.4.4.2 thorpej #ifdef UMASS_DEBUG
442 1.4.4.2 thorpej struct timeval tv;
443 1.4.4.2 thorpej u_int delta;
444 1.4.4.2 thorpej microtime(&tv);
445 1.4.4.2 thorpej delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
446 1.4.4.2 thorpej #endif
447 1.4.4.2 thorpej
448 1.4.4.2 thorpej DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu, delta=%u: xs=%p residue=%d"
449 1.4.4.2 thorpej " status=%d\n", tv.tv_sec, tv.tv_usec, delta, xs, residue, status));
450 1.4.4.2 thorpej
451 1.4.4.2 thorpej xs->resid = residue;
452 1.4.4.2 thorpej
453 1.4.4.2 thorpej switch (status) {
454 1.4.4.2 thorpej case STATUS_CMD_OK:
455 1.4.4.2 thorpej xs->error = XS_NOERROR;
456 1.4.4.2 thorpej break;
457 1.4.4.2 thorpej
458 1.4.4.2 thorpej case STATUS_CMD_UNKNOWN:
459 1.4.4.2 thorpej /* we can't issue REQUEST SENSE */
460 1.4.4.2 thorpej if (xs->xs_periph->periph_quirks & PQUIRK_NOSENSE) {
461 1.4.4.2 thorpej /*
462 1.4.4.2 thorpej * If no residue and no other USB error,
463 1.4.4.2 thorpej * command succeeded.
464 1.4.4.2 thorpej */
465 1.4.4.2 thorpej if (residue == 0) {
466 1.4.4.2 thorpej xs->error = XS_NOERROR;
467 1.4.4.2 thorpej break;
468 1.4.4.2 thorpej }
469 1.4.4.2 thorpej
470 1.4.4.2 thorpej /*
471 1.4.4.2 thorpej * Some devices return a short INQUIRY
472 1.4.4.2 thorpej * response, omitting response data from the
473 1.4.4.2 thorpej * "vendor specific data" on...
474 1.4.4.2 thorpej */
475 1.4.4.2 thorpej if (xs->cmd->opcode == INQUIRY &&
476 1.4.4.2 thorpej residue < xs->datalen) {
477 1.4.4.2 thorpej xs->error = XS_NOERROR;
478 1.4.4.2 thorpej break;
479 1.4.4.2 thorpej }
480 1.4.4.2 thorpej
481 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
482 1.4.4.2 thorpej break;
483 1.4.4.2 thorpej }
484 1.4.4.2 thorpej /* FALLTHROUGH */
485 1.4.4.2 thorpej case STATUS_CMD_FAILED:
486 1.4.4.2 thorpej /* fetch sense data */
487 1.4.4.2 thorpej memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
488 1.4.4.2 thorpej scbus->sc_sense_cmd.opcode = REQUEST_SENSE;
489 1.4.4.2 thorpej scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
490 1.4.4.2 thorpej SCSI_CMD_LUN_SHIFT;
491 1.4.4.2 thorpej scbus->sc_sense_cmd.length = sizeof(xs->sense);
492 1.4.4.2 thorpej
493 1.4.4.2 thorpej cmdlen = sizeof(scbus->sc_sense_cmd);
494 1.4.4.2 thorpej if (sc->sc_cmd == UMASS_CPROTO_UFI) /* XXX */
495 1.4.4.2 thorpej cmdlen = UFI_COMMAND_LENGTH;
496 1.4.4.2 thorpej sc->sc_methods->wire_xfer(sc, periph->periph_lun,
497 1.4.4.2 thorpej &scbus->sc_sense_cmd, cmdlen,
498 1.4.4.2 thorpej &xs->sense, sizeof(xs->sense),
499 1.4.4.2 thorpej DIR_IN, xs->timeout,
500 1.4.4.2 thorpej umass_scsipi_sense_cb, xs);
501 1.4.4.2 thorpej return;
502 1.4.4.2 thorpej
503 1.4.4.2 thorpej case STATUS_WIRE_FAILED:
504 1.4.4.2 thorpej xs->error = XS_RESET;
505 1.4.4.2 thorpej break;
506 1.4.4.2 thorpej
507 1.4.4.2 thorpej default:
508 1.4.4.2 thorpej panic("%s: Unknown status %d in umass_scsipi_cb\n",
509 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev), status);
510 1.4.4.2 thorpej }
511 1.4.4.2 thorpej
512 1.4.4.2 thorpej DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu: return xs->error="
513 1.4.4.2 thorpej "%d, xs->xs_status=0x%x xs->resid=%d\n",
514 1.4.4.2 thorpej tv.tv_sec, tv.tv_usec,
515 1.4.4.2 thorpej xs->error, xs->xs_status, xs->resid));
516 1.4.4.2 thorpej
517 1.4.4.2 thorpej s = splbio();
518 1.4.4.2 thorpej scsipi_done(xs);
519 1.4.4.2 thorpej splx(s);
520 1.4.4.2 thorpej }
521 1.4.4.2 thorpej
522 1.4.4.2 thorpej /*
523 1.4.4.2 thorpej * Finalise a completed autosense operation
524 1.4.4.2 thorpej */
525 1.4.4.2 thorpej Static void
526 1.4.4.2 thorpej umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
527 1.4.4.2 thorpej int status)
528 1.4.4.2 thorpej {
529 1.4.4.2 thorpej struct scsipi_xfer *xs = priv;
530 1.4.4.2 thorpej int s;
531 1.4.4.2 thorpej
532 1.4.4.2 thorpej DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
533 1.4.4.2 thorpej "status=%d\n", xs, residue, status));
534 1.4.4.2 thorpej
535 1.4.4.2 thorpej switch (status) {
536 1.4.4.2 thorpej case STATUS_CMD_OK:
537 1.4.4.2 thorpej case STATUS_CMD_UNKNOWN:
538 1.4.4.2 thorpej /* getting sense data succeeded */
539 1.4.4.2 thorpej if (xs->cmd->opcode == INQUIRY && (xs->resid < xs->datalen ||
540 1.4.4.2 thorpej (sc->sc_quirks & UMASS_QUIRK_RS_NO_CLEAR_UA /* XXX */))) {
541 1.4.4.2 thorpej /*
542 1.4.4.2 thorpej * Some drivers return SENSE errors even after INQUIRY.
543 1.4.4.2 thorpej * The upper layer doesn't like that.
544 1.4.4.2 thorpej */
545 1.4.4.2 thorpej xs->error = XS_NOERROR;
546 1.4.4.2 thorpej break;
547 1.4.4.2 thorpej }
548 1.4.4.2 thorpej /* XXX look at residue */
549 1.4.4.2 thorpej if (residue == 0 || residue == 14)/* XXX */
550 1.4.4.2 thorpej xs->error = XS_SENSE;
551 1.4.4.2 thorpej else
552 1.4.4.2 thorpej xs->error = XS_SHORTSENSE;
553 1.4.4.2 thorpej break;
554 1.4.4.2 thorpej default:
555 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
556 1.4.4.2 thorpej USBDEVNAME(sc->sc_dev), status));
557 1.4.4.2 thorpej xs->error = XS_DRIVER_STUFFUP;
558 1.4.4.2 thorpej break;
559 1.4.4.2 thorpej }
560 1.4.4.2 thorpej
561 1.4.4.2 thorpej xs->xs_status |= XS_STS_DONE;
562 1.4.4.2 thorpej
563 1.4.4.2 thorpej DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
564 1.4.4.2 thorpej "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
565 1.4.4.2 thorpej xs->resid));
566 1.4.4.2 thorpej
567 1.4.4.2 thorpej s = splbio();
568 1.4.4.2 thorpej scsipi_done(xs);
569 1.4.4.2 thorpej splx(s);
570 1.4.4.2 thorpej }
571 1.4.4.2 thorpej
572 1.4.4.2 thorpej #if NATAPIBUS > 0
573 1.4.4.2 thorpej Static void
574 1.4.4.2 thorpej umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
575 1.4.4.2 thorpej {
576 1.4.4.2 thorpej struct scsipi_channel *chan = atapi->sc_channel;
577 1.4.4.2 thorpej struct scsipi_periph *periph;
578 1.4.4.2 thorpej struct scsipibus_attach_args sa;
579 1.4.4.2 thorpej char vendor[33], product[65], revision[17];
580 1.4.4.2 thorpej struct scsipi_inquiry_data inqbuf;
581 1.4.4.2 thorpej
582 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
583 1.4.4.2 thorpej atapi, target));
584 1.4.4.2 thorpej
585 1.4.4.2 thorpej if (target != UMASS_ATAPI_DRIVE) /* only probe drive 0 */
586 1.4.4.2 thorpej return;
587 1.4.4.2 thorpej
588 1.4.4.2 thorpej /* skip if already attached */
589 1.4.4.2 thorpej if (scsipi_lookup_periph(chan, target, 0) != NULL)
590 1.4.4.2 thorpej return;
591 1.4.4.2 thorpej
592 1.4.4.2 thorpej periph = scsipi_alloc_periph(M_NOWAIT);
593 1.4.4.2 thorpej if (periph == NULL) {
594 1.4.4.2 thorpej printf("%s: can't allocate link for drive %d\n",
595 1.4.4.2 thorpej atapi->sc_dev.dv_xname, target);
596 1.4.4.2 thorpej return;
597 1.4.4.2 thorpej }
598 1.4.4.2 thorpej
599 1.4.4.2 thorpej DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
600 1.4.4.2 thorpej periph->periph_channel = chan;
601 1.4.4.2 thorpej periph->periph_switch = &atapi_probe_periphsw;
602 1.4.4.2 thorpej periph->periph_target = target;
603 1.4.4.2 thorpej periph->periph_quirks = chan->chan_defquirks;
604 1.4.4.2 thorpej
605 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
606 1.4.4.2 thorpej /* Now go ask the device all about itself. */
607 1.4.4.2 thorpej memset(&inqbuf, 0, sizeof(inqbuf));
608 1.4.4.2 thorpej if (scsipi_inquire(periph, &inqbuf,
609 1.4.4.2 thorpej XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0) {
610 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: "
611 1.4.4.2 thorpej "scsipi_inquire failed\n"));
612 1.4.4.2 thorpej free(periph, M_DEVBUF);
613 1.4.4.2 thorpej return;
614 1.4.4.2 thorpej }
615 1.4.4.2 thorpej
616 1.4.4.2 thorpej scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
617 1.4.4.2 thorpej scsipi_strvis(product, 65, inqbuf.product, 16);
618 1.4.4.2 thorpej scsipi_strvis(revision, 17, inqbuf.revision, 4);
619 1.4.4.2 thorpej
620 1.4.4.2 thorpej sa.sa_periph = periph;
621 1.4.4.2 thorpej sa.sa_inqbuf.type = inqbuf.device;
622 1.4.4.2 thorpej sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
623 1.4.4.2 thorpej T_REMOV : T_FIXED;
624 1.4.4.2 thorpej if (sa.sa_inqbuf.removable)
625 1.4.4.2 thorpej periph->periph_flags |= PERIPH_REMOVABLE;
626 1.4.4.2 thorpej sa.sa_inqbuf.vendor = vendor;
627 1.4.4.2 thorpej sa.sa_inqbuf.product = product;
628 1.4.4.2 thorpej sa.sa_inqbuf.revision = revision;
629 1.4.4.2 thorpej sa.sa_inqptr = NULL;
630 1.4.4.2 thorpej
631 1.4.4.2 thorpej DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on "
632 1.4.4.2 thorpej "'%s' '%s' '%s'\n", vendor, product, revision));
633 1.4.4.2 thorpej atapi_probe_device(atapi, target, periph, &sa);
634 1.4.4.2 thorpej /* atapi_probe_device() frees the periph when there is no device.*/
635 1.4.4.2 thorpej }
636 1.4.4.2 thorpej #endif
637