umass.c revision 1.21.2.6 1 1.21.2.4 bouyer /* $NetBSD: umass.c,v 1.21.2.6 2001/01/05 17:36:32 bouyer Exp $ */
2 1.1 thorpej /*-
3 1.1 thorpej * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
4 1.21.2.3 bouyer * Nick Hibma <n_hibma (at) freebsd.org>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 thorpej * SUCH DAMAGE.
27 1.1 thorpej *
28 1.21.2.3 bouyer * $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
29 1.1 thorpej */
30 1.1 thorpej
31 1.1 thorpej /*
32 1.21.2.6 bouyer * Universal Serial Bus Mass Storage Class specs:
33 1.21.2.6 bouyer * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
34 1.21.2.6 bouyer * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
35 1.21.2.6 bouyer * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
36 1.21.2.6 bouyer * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
37 1.21.2.3 bouyer */
38 1.21.2.3 bouyer
39 1.21.2.3 bouyer /*
40 1.21.2.3 bouyer * Ported to NetBSD by Lennart Augustsson <augustss (at) netbsd.org>.
41 1.21.2.3 bouyer * Parts of the code written my Jason R. Thorpe <thorpej (at) shagadelic.org>.
42 1.21.2.3 bouyer */
43 1.21.2.3 bouyer
44 1.21.2.3 bouyer /*
45 1.21.2.3 bouyer * The driver handles 3 Wire Protocols
46 1.21.2.3 bouyer * - Command/Bulk/Interrupt (CBI)
47 1.21.2.3 bouyer * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
48 1.21.2.3 bouyer * - Mass Storage Bulk-Only (BBB)
49 1.21.2.3 bouyer * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
50 1.3 thorpej *
51 1.21.2.3 bouyer * Over these wire protocols it handles the following command protocols
52 1.21.2.3 bouyer * - SCSI
53 1.21.2.3 bouyer * - UFI (floppy command set)
54 1.21.2.3 bouyer * - 8070 (ATA/ATAPI)
55 1.3 thorpej *
56 1.21.2.3 bouyer * UFI and 8070i are transformed versions of the SCSI command set. The
57 1.21.2.3 bouyer * sc->transform method is used to convert the commands into the appropriate
58 1.21.2.3 bouyer * format (if at all necessary). For example, UFI requires all commands to be
59 1.21.2.3 bouyer * 12 bytes in length amongst other things.
60 1.3 thorpej *
61 1.21.2.3 bouyer * The source code below is marked and can be split into a number of pieces
62 1.21.2.3 bouyer * (in this order):
63 1.1 thorpej *
64 1.21.2.3 bouyer * - probe/attach/detach
65 1.21.2.3 bouyer * - generic transfer routines
66 1.21.2.3 bouyer * - BBB
67 1.21.2.3 bouyer * - CBI
68 1.21.2.3 bouyer * - CBI_I (in addition to functions from CBI)
69 1.21.2.3 bouyer * - CAM (Common Access Method)
70 1.21.2.3 bouyer * - SCSI
71 1.21.2.3 bouyer * - UFI
72 1.21.2.3 bouyer * - 8070i
73 1.2 thorpej *
74 1.21.2.3 bouyer * The protocols are implemented using a state machine, for the transfers as
75 1.21.2.3 bouyer * well as for the resets. The state machine is contained in umass_*_state.
76 1.21.2.3 bouyer * The state machine is started through either umass_*_transfer or
77 1.21.2.3 bouyer * umass_*_reset.
78 1.3 thorpej *
79 1.21.2.3 bouyer * The reason for doing this is a) CAM performs a lot better this way and b) it
80 1.21.2.3 bouyer * avoids using tsleep from interrupt context (for example after a failed
81 1.21.2.3 bouyer * transfer).
82 1.21.2.3 bouyer */
83 1.21.2.3 bouyer
84 1.21.2.3 bouyer /*
85 1.21.2.3 bouyer * The SCSI related part of this driver has been derived from the
86 1.21.2.3 bouyer * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch (at) freebsd.org).
87 1.6 thorpej *
88 1.21.2.3 bouyer * The CAM layer uses so called actions which are messages sent to the host
89 1.21.2.3 bouyer * adapter for completion. The actions come in through umass_cam_action. The
90 1.21.2.3 bouyer * appropriate block of routines is called depending on the transport protocol
91 1.21.2.3 bouyer * in use. When the transfer has finished, these routines call
92 1.21.2.3 bouyer * umass_cam_cb again to complete the CAM command.
93 1.1 thorpej */
94 1.1 thorpej
95 1.21.2.3 bouyer /* XXX Should we split the driver into a number of files? umass.c,
96 1.21.2.3 bouyer * umass_scsi.c, umass_8070.c, umass_ufi.c, umass_bbb.c, umass_cbi.c or
97 1.21.2.3 bouyer * something similar?
98 1.1 thorpej */
99 1.1 thorpej
100 1.21.2.3 bouyer #include "atapibus.h"
101 1.21.2.3 bouyer
102 1.1 thorpej #include <sys/param.h>
103 1.1 thorpej #include <sys/systm.h>
104 1.1 thorpej #include <sys/kernel.h>
105 1.21.2.3 bouyer #include <sys/conf.h>
106 1.21.2.3 bouyer #if defined(__NetBSD__) || defined(__OpenBSD__)
107 1.1 thorpej #include <sys/buf.h>
108 1.21.2.3 bouyer #include <sys/device.h>
109 1.21.2.3 bouyer #include <sys/ioctl.h>
110 1.21.2.3 bouyer #include <sys/malloc.h>
111 1.21.2.3 bouyer #undef KASSERT
112 1.21.2.3 bouyer #define KASSERT(cond, msg)
113 1.21.2.3 bouyer #elif defined(__FreeBSD__)
114 1.21.2.3 bouyer #include <sys/module.h>
115 1.21.2.3 bouyer #include <sys/bus.h>
116 1.21.2.3 bouyer #include <machine/clock.h>
117 1.21.2.3 bouyer #endif
118 1.1 thorpej
119 1.1 thorpej #include <dev/usb/usb.h>
120 1.1 thorpej #include <dev/usb/usbdi.h>
121 1.1 thorpej #include <dev/usb/usbdi_util.h>
122 1.21.2.3 bouyer #include <dev/usb/usbdevs.h>
123 1.21.2.3 bouyer
124 1.21.2.3 bouyer #if defined(__FreeBSD__)
125 1.21.2.3 bouyer #include <cam/cam.h>
126 1.21.2.3 bouyer #include <cam/cam_ccb.h>
127 1.21.2.3 bouyer #include <cam/cam_sim.h>
128 1.21.2.3 bouyer #include <cam/cam_xpt_sim.h>
129 1.21.2.3 bouyer #include <cam/scsi/scsi_all.h>
130 1.21.2.3 bouyer #include <cam/scsi/scsi_da.h>
131 1.21.2.3 bouyer
132 1.21.2.3 bouyer #ifdef UMASS_DO_CAM_RESCAN
133 1.21.2.3 bouyer #include <sys/devicestat.h>
134 1.21.2.3 bouyer #include <cam/cam_periph.h>
135 1.21.2.3 bouyer #endif
136 1.1 thorpej
137 1.21.2.3 bouyer #elif defined(__NetBSD__) || defined(__OpenBSD__)
138 1.21.2.3 bouyer #include <sys/scsiio.h>
139 1.1 thorpej #include <dev/scsipi/scsi_all.h>
140 1.1 thorpej #include <dev/scsipi/scsipi_all.h>
141 1.21.2.3 bouyer #include <dev/scsipi/scsiconf.h>
142 1.21.2.3 bouyer
143 1.21.2.3 bouyer #include <dev/scsipi/atapiconf.h>
144 1.1 thorpej
145 1.21.2.3 bouyer #include <dev/scsipi/scsipi_disk.h>
146 1.21.2.3 bouyer #include <dev/scsipi/scsi_disk.h>
147 1.21.2.3 bouyer #include <dev/scsipi/scsi_changer.h>
148 1.21.2.3 bouyer
149 1.21.2.3 bouyer #include <dev/ata/atavar.h> /* XXX */
150 1.21.2.3 bouyer #include <sys/disk.h> /* XXX */
151 1.21.2.3 bouyer #include <dev/scsipi/sdvar.h> /* XXX */
152 1.18 augustss #endif
153 1.18 augustss
154 1.1 thorpej #ifdef UMASS_DEBUG
155 1.21.2.3 bouyer #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
156 1.21.2.3 bouyer #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x
157 1.21.2.3 bouyer #define UDMASS_UPPER 0x00008000 /* upper layer */
158 1.21.2.3 bouyer #define UDMASS_GEN 0x00010000 /* general */
159 1.21.2.3 bouyer #define UDMASS_SCSI 0x00020000 /* scsi */
160 1.21.2.3 bouyer #define UDMASS_UFI 0x00040000 /* ufi command set */
161 1.21.2.3 bouyer #define UDMASS_8070 0x00080000 /* 8070i command set */
162 1.21.2.3 bouyer #define UDMASS_USB 0x00100000 /* USB general */
163 1.21.2.3 bouyer #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
164 1.21.2.3 bouyer #define UDMASS_CBI 0x00400000 /* CBI transfers */
165 1.21.2.3 bouyer #define UDMASS_ALL 0xffff0000 /* all of the above */
166 1.21.2.3 bouyer
167 1.21.2.3 bouyer #define UDMASS_XFER 0x40000000 /* all transfers */
168 1.21.2.3 bouyer #define UDMASS_CMD 0x80000000
169 1.21.2.3 bouyer
170 1.21.2.3 bouyer int umassdebug = 0;
171 1.1 thorpej #else
172 1.21.2.3 bouyer #define DIF(m, x) /* nop */
173 1.21.2.3 bouyer #define DPRINTF(m, x) /* nop */
174 1.1 thorpej #endif
175 1.1 thorpej
176 1.1 thorpej
177 1.21.2.3 bouyer /* Generic definitions */
178 1.17 thorpej
179 1.21.2.3 bouyer #define UFI_COMMAND_LENGTH 12
180 1.1 thorpej
181 1.21.2.3 bouyer /* Direction for umass_*_transfer */
182 1.21.2.3 bouyer #define DIR_NONE 0
183 1.21.2.3 bouyer #define DIR_IN 1
184 1.21.2.3 bouyer #define DIR_OUT 2
185 1.9 thorpej
186 1.21.2.3 bouyer /* The transfer speed determines the timeout value */
187 1.21.2.3 bouyer #define UMASS_DEFAULT_TRANSFER_SPEED 150 /* in kb/s, conservative est. */
188 1.21.2.3 bouyer #define UMASS_FLOPPY_TRANSFER_SPEED 20
189 1.21.2.3 bouyer #define UMASS_ZIP100_TRANSFER_SPEED 650
190 1.9 thorpej
191 1.21.2.3 bouyer #define UMASS_SPINUP_TIME 10000 /* ms */
192 1.1 thorpej
193 1.21.2.3 bouyer #ifdef __FreeBSD__
194 1.21.2.3 bouyer /* device name */
195 1.21.2.3 bouyer #define DEVNAME "umass"
196 1.21.2.3 bouyer #define DEVNAME_SIM "umass-"
197 1.1 thorpej
198 1.21.2.3 bouyer #define UMASS_MAX_TRANSFER_SIZE 65536
199 1.21.2.3 bouyer
200 1.21.2.3 bouyer /* CAM specific definitions */
201 1.21.2.3 bouyer
202 1.21.2.3 bouyer /* The bus id, whatever that is */
203 1.21.2.3 bouyer #define UMASS_SCSI_BUS 0
204 1.21.2.3 bouyer
205 1.21.2.3 bouyer /* All USB drives are 'connected' to one SIM (SCSI controller). umass3
206 1.21.2.3 bouyer * ends up being target 3 on that SIM. When a request for target 3
207 1.21.2.3 bouyer * comes in we fetch the softc with devclass_get_softc(target_id).
208 1.21.2.3 bouyer *
209 1.21.2.3 bouyer * The SIM is the highest target number. This makes sure that umass0 corresponds
210 1.21.2.3 bouyer * to target 0 on the USB SCSI bus.
211 1.21.2.3 bouyer */
212 1.21.2.3 bouyer #ifndef UMASS_DEBUG
213 1.21.2.3 bouyer #define UMASS_SCSIID_MAX 32 /* maximum number of drives expected */
214 1.21.2.3 bouyer #else
215 1.21.2.3 bouyer /* while debugging avoid unnecessary clutter in the output at umass_cam_rescan
216 1.21.2.3 bouyer * (XPT_PATH_INQ)
217 1.21.2.3 bouyer */
218 1.21.2.3 bouyer #define UMASS_SCSIID_MAX 3 /* maximum number of drives expected */
219 1.21.2.3 bouyer #endif
220 1.21.2.3 bouyer #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
221 1.21.2.3 bouyer #endif
222 1.21.2.3 bouyer
223 1.21.2.3 bouyer #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
224 1.1 thorpej
225 1.1 thorpej
226 1.21.2.3 bouyer /* Bulk-Only features */
227 1.21.2.3 bouyer
228 1.21.2.3 bouyer #define UR_BBB_RESET 0xff /* Bulk-Only reset */
229 1.21.2.3 bouyer #define UR_BBB_GET_MAX_LUN 0xfe
230 1.1 thorpej
231 1.1 thorpej /* Command Block Wrapper */
232 1.1 thorpej typedef struct {
233 1.1 thorpej uDWord dCBWSignature;
234 1.21.2.3 bouyer # define CBWSIGNATURE 0x43425355
235 1.1 thorpej uDWord dCBWTag;
236 1.1 thorpej uDWord dCBWDataTransferLength;
237 1.1 thorpej uByte bCBWFlags;
238 1.21.2.3 bouyer # define CBWFLAGS_OUT 0x00
239 1.21.2.3 bouyer # define CBWFLAGS_IN 0x80
240 1.1 thorpej uByte bCBWLUN;
241 1.1 thorpej uByte bCDBLength;
242 1.21.2.3 bouyer # define CBWCDBLENGTH 16
243 1.21.2.3 bouyer uByte CBWCDB[CBWCDBLENGTH];
244 1.21.2.3 bouyer } umass_bbb_cbw_t;
245 1.21.2.3 bouyer #define UMASS_BBB_CBW_SIZE 31
246 1.1 thorpej
247 1.1 thorpej /* Command Status Wrapper */
248 1.1 thorpej typedef struct {
249 1.1 thorpej uDWord dCSWSignature;
250 1.21.2.3 bouyer # define CSWSIGNATURE 0x53425355
251 1.1 thorpej uDWord dCSWTag;
252 1.1 thorpej uDWord dCSWDataResidue;
253 1.1 thorpej uByte bCSWStatus;
254 1.21.2.3 bouyer # define CSWSTATUS_GOOD 0x0
255 1.21.2.3 bouyer # define CSWSTATUS_FAILED 0x1
256 1.21.2.3 bouyer # define CSWSTATUS_PHASE 0x2
257 1.21.2.3 bouyer } umass_bbb_csw_t;
258 1.21.2.3 bouyer #define UMASS_BBB_CSW_SIZE 13
259 1.21.2.3 bouyer
260 1.21.2.3 bouyer /* CBI features */
261 1.21.2.3 bouyer
262 1.21.2.3 bouyer #define UR_CBI_ADSC 0x00
263 1.21.2.3 bouyer
264 1.21.2.3 bouyer typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
265 1.21.2.3 bouyer
266 1.21.2.3 bouyer typedef union {
267 1.21.2.3 bouyer struct {
268 1.21.2.3 bouyer unsigned char type;
269 1.21.2.3 bouyer #define IDB_TYPE_CCI 0x00
270 1.21.2.3 bouyer unsigned char value;
271 1.21.2.3 bouyer #define IDB_VALUE_PASS 0x00
272 1.21.2.3 bouyer #define IDB_VALUE_FAIL 0x01
273 1.21.2.3 bouyer #define IDB_VALUE_PHASE 0x02
274 1.21.2.3 bouyer #define IDB_VALUE_PERSISTENT 0x03
275 1.21.2.3 bouyer #define IDB_VALUE_STATUS_MASK 0x03
276 1.21.2.3 bouyer } common;
277 1.21.2.3 bouyer
278 1.21.2.3 bouyer struct {
279 1.21.2.3 bouyer unsigned char asc;
280 1.21.2.3 bouyer unsigned char ascq;
281 1.21.2.3 bouyer } ufi;
282 1.21.2.3 bouyer } umass_cbi_sbl_t;
283 1.21.2.3 bouyer
284 1.21.2.3 bouyer
285 1.21.2.3 bouyer
286 1.21.2.3 bouyer struct umass_softc; /* see below */
287 1.21.2.3 bouyer
288 1.21.2.3 bouyer typedef void (*transfer_cb_f)(struct umass_softc *sc, void *priv,
289 1.21.2.3 bouyer int residue, int status);
290 1.21.2.3 bouyer #define STATUS_CMD_OK 0 /* everything ok */
291 1.21.2.3 bouyer #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
292 1.21.2.3 bouyer #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
293 1.21.2.3 bouyer #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
294 1.21.2.3 bouyer
295 1.21.2.3 bouyer typedef void (*wire_reset_f)(struct umass_softc *sc, int status);
296 1.21.2.3 bouyer typedef void (*wire_transfer_f)(struct umass_softc *sc, int lun,
297 1.21.2.3 bouyer void *cmd, int cmdlen, void *data, int datalen,
298 1.21.2.3 bouyer int dir, transfer_cb_f cb, void *priv);
299 1.21.2.3 bouyer typedef void (*wire_state_f)(usbd_xfer_handle xfer,
300 1.21.2.3 bouyer usbd_private_handle priv, usbd_status err);
301 1.21.2.3 bouyer
302 1.21.2.3 bouyer #if defined(__FreeBSD__)
303 1.21.2.3 bouyer typedef int (*command_transform_f)(struct umass_softc *sc,
304 1.21.2.3 bouyer u_char *cmd, int cmdlen,
305 1.21.2.3 bouyer u_char **rcmd, int *rcmdlen));
306 1.21.2.3 bouyer #endif
307 1.21.2.3 bouyer
308 1.21.2.3 bouyer
309 1.21.2.3 bouyer /* the per device structure */
310 1.21.2.3 bouyer struct umass_softc {
311 1.21.2.3 bouyer USBBASEDEVICE sc_dev; /* base device */
312 1.21.2.3 bouyer usbd_device_handle sc_udev; /* device */
313 1.1 thorpej
314 1.21.2.3 bouyer unsigned char drive;
315 1.21.2.3 bouyer # define DRIVE_GENERIC 0 /* use defaults for this one */
316 1.21.2.3 bouyer # define ZIP_100 1 /* to be used for quirks */
317 1.21.2.3 bouyer # define ZIP_250 2
318 1.21.2.3 bouyer # define SHUTTLE_EUSB 3
319 1.21.2.3 bouyer # define INSYSTEM_USBCABLE 4
320 1.21.2.3 bouyer unsigned char quirks;
321 1.21.2.3 bouyer /* The drive does not support Test Unit Ready. Convert to
322 1.21.2.3 bouyer * Start Unit.
323 1.21.2.3 bouyer * Y-E Data
324 1.21.2.3 bouyer * ZIP 100
325 1.21.2.3 bouyer */
326 1.21.2.3 bouyer # define NO_TEST_UNIT_READY 0x01
327 1.21.2.3 bouyer /* The drive does not reset the Unit Attention state after
328 1.21.2.3 bouyer * REQUEST SENSE has been sent. The INQUIRY command does not reset
329 1.21.2.3 bouyer * the UA either, and so CAM runs in circles trying to retrieve the
330 1.21.2.3 bouyer * initial INQUIRY data.
331 1.21.2.3 bouyer * Y-E Data
332 1.21.2.3 bouyer */
333 1.21.2.3 bouyer # define RS_NO_CLEAR_UA 0x02 /* no REQUEST SENSE on INQUIRY*/
334 1.21.2.3 bouyer /* The drive does not support START_STOP.
335 1.21.2.3 bouyer * Shuttle E-USB
336 1.21.2.3 bouyer */
337 1.21.2.3 bouyer # define NO_START_STOP 0x04
338 1.21.2.3 bouyer
339 1.21.2.3 bouyer unsigned int proto;
340 1.21.2.3 bouyer # define PROTO_UNKNOWN 0x0000 /* unknown protocol */
341 1.21.2.3 bouyer # define PROTO_BBB 0x0001 /* USB wire protocol */
342 1.21.2.3 bouyer # define PROTO_CBI 0x0002
343 1.21.2.3 bouyer # define PROTO_CBI_I 0x0004
344 1.21.2.3 bouyer # define PROTO_WIRE 0x00ff /* USB wire protocol mask */
345 1.21.2.3 bouyer # define PROTO_SCSI 0x0100 /* command protocol */
346 1.21.2.3 bouyer # define PROTO_ATAPI 0x0200
347 1.21.2.3 bouyer # define PROTO_UFI 0x0400
348 1.21.2.3 bouyer # define PROTO_RBC 0x0800
349 1.21.2.3 bouyer # define PROTO_COMMAND 0xff00 /* command protocol mask */
350 1.21.2.3 bouyer
351 1.21.2.3 bouyer u_char subclass; /* interface subclass */
352 1.21.2.3 bouyer u_char protocol; /* interface protocol */
353 1.21.2.3 bouyer
354 1.21.2.3 bouyer usbd_interface_handle iface; /* Mass Storage interface */
355 1.21.2.3 bouyer int ifaceno; /* MS iface number */
356 1.21.2.3 bouyer
357 1.21.2.3 bouyer u_int8_t bulkin; /* bulk-in Endpoint Address */
358 1.21.2.3 bouyer u_int8_t bulkout; /* bulk-out Endpoint Address */
359 1.21.2.3 bouyer u_int8_t intrin; /* intr-in Endp. (CBI) */
360 1.21.2.3 bouyer usbd_pipe_handle bulkin_pipe;
361 1.21.2.3 bouyer usbd_pipe_handle bulkout_pipe;
362 1.21.2.3 bouyer usbd_pipe_handle intrin_pipe;
363 1.21.2.3 bouyer
364 1.21.2.3 bouyer /* Reset the device in a wire protocol specific way */
365 1.21.2.3 bouyer wire_reset_f reset;
366 1.21.2.3 bouyer
367 1.21.2.3 bouyer /* The start of a wire transfer. It prepares the whole transfer (cmd,
368 1.21.2.3 bouyer * data, and status stage) and initiates it. It is up to the state
369 1.21.2.3 bouyer * machine (below) to handle the various stages and errors in these
370 1.21.2.3 bouyer */
371 1.21.2.3 bouyer wire_transfer_f transfer;
372 1.21.2.3 bouyer
373 1.21.2.3 bouyer /* The state machine, handling the various states during a transfer */
374 1.21.2.3 bouyer wire_state_f state;
375 1.21.2.3 bouyer
376 1.21.2.3 bouyer #if defined(__FreeBSD__)
377 1.21.2.3 bouyer /* The command transform function is used to conver the SCSI commands
378 1.21.2.3 bouyer * into their derivatives, like UFI, ATAPI, and friends.
379 1.21.2.3 bouyer */
380 1.21.2.3 bouyer command_transform_f transform; /* command transform */
381 1.21.2.3 bouyer #endif
382 1.21.2.3 bouyer
383 1.21.2.3 bouyer /* Bulk specific variables for transfers in progress */
384 1.21.2.3 bouyer umass_bbb_cbw_t cbw; /* command block wrapper */
385 1.21.2.3 bouyer umass_bbb_csw_t csw; /* command status wrapper*/
386 1.21.2.3 bouyer /* CBI specific variables for transfers in progress */
387 1.21.2.3 bouyer umass_cbi_cbl_t cbl; /* command block */
388 1.21.2.3 bouyer umass_cbi_sbl_t sbl; /* status block */
389 1.21.2.3 bouyer
390 1.21.2.3 bouyer /* generic variables for transfers in progress */
391 1.21.2.3 bouyer /* ctrl transfer requests */
392 1.21.2.3 bouyer usb_device_request_t request;
393 1.21.2.3 bouyer
394 1.21.2.3 bouyer /* xfer handles
395 1.21.2.3 bouyer * Most of our operations are initiated from interrupt context, so
396 1.21.2.3 bouyer * we need to avoid using the one that is in use. We want to avoid
397 1.21.2.3 bouyer * allocating them in the interrupt context as well.
398 1.21.2.3 bouyer */
399 1.21.2.3 bouyer /* indices into array below */
400 1.21.2.3 bouyer # define XFER_BBB_CBW 0 /* Bulk-Only */
401 1.21.2.3 bouyer # define XFER_BBB_DATA 1
402 1.21.2.3 bouyer # define XFER_BBB_DCLEAR 2
403 1.21.2.3 bouyer # define XFER_BBB_CSW1 3
404 1.21.2.3 bouyer # define XFER_BBB_CSW2 4
405 1.21.2.3 bouyer # define XFER_BBB_SCLEAR 5
406 1.21.2.3 bouyer # define XFER_BBB_RESET1 6
407 1.21.2.3 bouyer # define XFER_BBB_RESET2 7
408 1.21.2.3 bouyer # define XFER_BBB_RESET3 8
409 1.21.2.3 bouyer
410 1.21.2.3 bouyer # define XFER_CBI_CB 0 /* CBI */
411 1.21.2.3 bouyer # define XFER_CBI_DATA 1
412 1.21.2.3 bouyer # define XFER_CBI_STATUS 2
413 1.21.2.3 bouyer # define XFER_CBI_DCLEAR 3
414 1.21.2.3 bouyer # define XFER_CBI_SCLEAR 4
415 1.21.2.3 bouyer # define XFER_CBI_RESET1 5
416 1.21.2.3 bouyer # define XFER_CBI_RESET2 6
417 1.21.2.3 bouyer # define XFER_CBI_RESET3 7
418 1.21.2.3 bouyer
419 1.21.2.3 bouyer # define XFER_NR 9 /* maximum number */
420 1.21.2.3 bouyer
421 1.21.2.3 bouyer usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
422 1.21.2.3 bouyer
423 1.21.2.3 bouyer void *data_buffer;
424 1.21.2.3 bouyer
425 1.21.2.3 bouyer int transfer_dir; /* data direction */
426 1.21.2.3 bouyer void *transfer_data; /* data buffer */
427 1.21.2.3 bouyer int transfer_datalen; /* (maximum) length */
428 1.21.2.3 bouyer int transfer_actlen; /* actual length */
429 1.21.2.3 bouyer transfer_cb_f transfer_cb; /* callback */
430 1.21.2.3 bouyer void *transfer_priv; /* for callback */
431 1.21.2.3 bouyer int transfer_status;
432 1.21.2.3 bouyer
433 1.21.2.3 bouyer int transfer_state;
434 1.21.2.3 bouyer # define TSTATE_IDLE 0
435 1.21.2.3 bouyer # define TSTATE_BBB_COMMAND 1 /* CBW transfer */
436 1.21.2.3 bouyer # define TSTATE_BBB_DATA 2 /* Data transfer */
437 1.21.2.3 bouyer # define TSTATE_BBB_DCLEAR 3 /* clear endpt stall */
438 1.21.2.3 bouyer # define TSTATE_BBB_STATUS1 4 /* clear endpt stall */
439 1.21.2.3 bouyer # define TSTATE_BBB_SCLEAR 5 /* clear endpt stall */
440 1.21.2.3 bouyer # define TSTATE_BBB_STATUS2 6 /* CSW transfer */
441 1.21.2.3 bouyer # define TSTATE_BBB_RESET1 7 /* reset command */
442 1.21.2.3 bouyer # define TSTATE_BBB_RESET2 8 /* in clear stall */
443 1.21.2.3 bouyer # define TSTATE_BBB_RESET3 9 /* out clear stall */
444 1.21.2.3 bouyer # define TSTATE_CBI_COMMAND 10 /* command transfer */
445 1.21.2.3 bouyer # define TSTATE_CBI_DATA 11 /* data transfer */
446 1.21.2.3 bouyer # define TSTATE_CBI_STATUS 12 /* status transfer */
447 1.21.2.3 bouyer # define TSTATE_CBI_DCLEAR 13 /* clear ep stall */
448 1.21.2.3 bouyer # define TSTATE_CBI_SCLEAR 14 /* clear ep stall */
449 1.21.2.3 bouyer # define TSTATE_CBI_RESET1 15 /* reset command */
450 1.21.2.3 bouyer # define TSTATE_CBI_RESET2 16 /* in clear stall */
451 1.21.2.3 bouyer # define TSTATE_CBI_RESET3 17 /* out clear stall */
452 1.21.2.3 bouyer # define TSTATE_STATES 18 /* # of states above */
453 1.21.2.3 bouyer
454 1.21.2.3 bouyer
455 1.21.2.3 bouyer int transfer_speed; /* in kb/s */
456 1.21.2.3 bouyer int timeout; /* in msecs */
457 1.21.2.3 bouyer
458 1.21.2.3 bouyer u_int8_t maxlun; /* max lun supported */
459 1.21.2.3 bouyer
460 1.21.2.3 bouyer #ifdef UMASS_DEBUG
461 1.21.2.3 bouyer struct timeval tv;
462 1.21.2.3 bouyer #endif
463 1.21.2.3 bouyer
464 1.21.2.3 bouyer #if defined(__FreeBSD__)
465 1.21.2.3 bouyer /* SCSI/CAM specific variables */
466 1.21.2.3 bouyer struct scsi_sense cam_scsi_sense;
467 1.21.2.3 bouyer
468 1.21.2.3 bouyer #elif defined(__NetBSD__) || defined(__OpenBSD__)
469 1.21.2.3 bouyer struct {
470 1.21.2.3 bouyer struct ata_atapi_attach sc_aa;
471 1.21.2.3 bouyer struct ata_drive_datas sc_aa_drive;
472 1.21.2.3 bouyer } aa;
473 1.21.2.3 bouyer struct atapi_adapter sc_atapi_adapter;
474 1.21.2.3 bouyer #define sc_adapter sc_atapi_adapter._generic
475 1.21.2.3 bouyer struct scsipi_channel sc_channel;
476 1.21.2.3 bouyer int sc_xfer_flags;
477 1.21.2.3 bouyer usbd_status sc_sync_status;
478 1.21.2.3 bouyer struct scsipi_sense sc_sense_cmd;
479 1.21.2.3 bouyer
480 1.21.2.3 bouyer device_ptr_t sc_child; /* child device, for detach */
481 1.21.2.3 bouyer char sc_dying;
482 1.21.2.3 bouyer
483 1.21.2.3 bouyer #endif
484 1.21.2.3 bouyer };
485 1.1 thorpej
486 1.21.2.3 bouyer #ifdef UMASS_DEBUG
487 1.21.2.3 bouyer char *states[TSTATE_STATES+1] = {
488 1.21.2.3 bouyer /* should be kept in sync with the list at transfer_state */
489 1.21.2.3 bouyer "Idle",
490 1.21.2.3 bouyer "BBB CBW",
491 1.21.2.3 bouyer "BBB Data",
492 1.21.2.3 bouyer "BBB Data bulk-in/-out clear stall",
493 1.21.2.3 bouyer "BBB CSW, 1st attempt",
494 1.21.2.3 bouyer "BBB CSW bulk-in clear stall",
495 1.21.2.3 bouyer "BBB CSW, 2nd attempt",
496 1.21.2.3 bouyer "BBB Reset",
497 1.21.2.3 bouyer "BBB bulk-in clear stall",
498 1.21.2.3 bouyer "BBB bulk-out clear stall",
499 1.21.2.3 bouyer "CBI Command",
500 1.21.2.3 bouyer "CBI Data",
501 1.21.2.3 bouyer "CBI Status",
502 1.21.2.3 bouyer "CBI Data bulk-in/-out clear stall",
503 1.21.2.3 bouyer "CBI Status intr-in clear stall",
504 1.21.2.3 bouyer "CBI Reset",
505 1.21.2.3 bouyer "CBI bulk-in clear stall",
506 1.21.2.3 bouyer "CBI bulk-out clear stall",
507 1.21.2.3 bouyer NULL
508 1.21.2.3 bouyer };
509 1.21.2.3 bouyer #endif
510 1.21.2.3 bouyer
511 1.21.2.3 bouyer struct cam_sim *umass_sim; /* SCSI Interface Module */
512 1.21.2.3 bouyer struct cam_path *umass_path; /* and its path */
513 1.21.2.3 bouyer
514 1.21.2.3 bouyer
515 1.21.2.3 bouyer /* USB device probe/attach/detach functions */
516 1.1 thorpej USB_DECLARE_DRIVER(umass);
517 1.21.2.3 bouyer Static void umass_disco(struct umass_softc *sc);
518 1.21.2.3 bouyer Static int umass_match_proto(struct umass_softc *sc,
519 1.21.2.3 bouyer usbd_interface_handle iface,
520 1.21.2.3 bouyer usbd_device_handle dev);
521 1.21.2.3 bouyer Static void umass_init_shuttle(struct umass_softc *sc);
522 1.1 thorpej
523 1.21.2.3 bouyer /* generic transfer functions */
524 1.21.2.3 bouyer Static usbd_status umass_setup_transfer(struct umass_softc *sc,
525 1.1 thorpej usbd_pipe_handle pipe,
526 1.21.2.3 bouyer void *buffer, int buflen, int flags,
527 1.21.2.3 bouyer usbd_xfer_handle xfer);
528 1.21.2.3 bouyer Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc,
529 1.21.2.3 bouyer usbd_device_handle dev,
530 1.21.2.3 bouyer usb_device_request_t *req,
531 1.21.2.3 bouyer void *buffer, int buflen, int flags,
532 1.21.2.3 bouyer usbd_xfer_handle xfer);
533 1.21.2.3 bouyer Static void umass_clear_endpoint_stall(struct umass_softc *sc,
534 1.21.2.3 bouyer u_int8_t endpt, usbd_pipe_handle pipe,
535 1.21.2.3 bouyer int state, usbd_xfer_handle xfer);
536 1.21.2.3 bouyer #if 0
537 1.21.2.3 bouyer Static void umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv);
538 1.21.2.3 bouyer #endif
539 1.1 thorpej
540 1.1 thorpej /* Bulk-Only related functions */
541 1.21.2.3 bouyer Static void umass_bbb_reset(struct umass_softc *sc, int status);
542 1.21.2.3 bouyer Static void umass_bbb_transfer(struct umass_softc *sc, int lun,
543 1.21.2.3 bouyer void *cmd, int cmdlen,
544 1.21.2.3 bouyer void *data, int datalen, int dir,
545 1.21.2.3 bouyer transfer_cb_f cb, void *priv);
546 1.21.2.3 bouyer Static void umass_bbb_state(usbd_xfer_handle xfer,
547 1.21.2.3 bouyer usbd_private_handle priv,
548 1.21.2.3 bouyer usbd_status err);
549 1.21.2.3 bouyer usbd_status umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun);
550 1.21.2.3 bouyer
551 1.21.2.3 bouyer
552 1.21.2.3 bouyer /* CBI related functions */
553 1.21.2.3 bouyer Static int umass_cbi_adsc(struct umass_softc *sc, char *buffer,int buflen,
554 1.21.2.3 bouyer usbd_xfer_handle xfer);
555 1.21.2.3 bouyer Static void umass_cbi_reset(struct umass_softc *sc, int status);
556 1.21.2.3 bouyer Static void umass_cbi_transfer(struct umass_softc *sc, int lun,
557 1.1 thorpej void *cmd, int cmdlen,
558 1.21.2.3 bouyer void *data, int datalen, int dir,
559 1.21.2.3 bouyer transfer_cb_f cb, void *priv);
560 1.21.2.3 bouyer Static void umass_cbi_state(usbd_xfer_handle xfer,
561 1.21.2.3 bouyer usbd_private_handle priv, usbd_status err);
562 1.21.2.3 bouyer
563 1.21.2.3 bouyer #if defined(__FreeBSD__)
564 1.21.2.3 bouyer /* CAM related functions */
565 1.21.2.3 bouyer Static void umass_cam_action(struct cam_sim *sim, union ccb *ccb);
566 1.21.2.3 bouyer Static void umass_cam_poll(struct cam_sim *sim);
567 1.21.2.3 bouyer
568 1.21.2.3 bouyer Static void umass_cam_cb(struct umass_softc *sc, void *priv,
569 1.21.2.3 bouyer int residue, int status);
570 1.21.2.3 bouyer Static void umass_cam_sense_cb(struct umass_softc *sc, void *priv,
571 1.21.2.3 bouyer int residue, int status);
572 1.21.2.3 bouyer
573 1.21.2.3 bouyer #ifdef UMASS_DO_CAM_RESCAN
574 1.21.2.3 bouyer Static void umass_cam_rescan(struct umass_softc *sc);
575 1.21.2.3 bouyer #endif
576 1.21.2.3 bouyer
577 1.21.2.3 bouyer Static int umass_cam_attach_sim(void);
578 1.21.2.3 bouyer Static int umass_cam_attach(struct umass_softc *sc);
579 1.21.2.3 bouyer Static int umass_cam_detach_sim(void);
580 1.21.2.3 bouyer Static int umass_cam_detach(struct umass_softc *sc);
581 1.21.2.3 bouyer
582 1.21.2.3 bouyer #elif defined(__NetBSD__) || defined(__OpenBSD__)
583 1.21.2.3 bouyer
584 1.21.2.3 bouyer #define UMASS_SCSIID_HOST 0x00
585 1.21.2.3 bouyer #define UMASS_SCSIID_DEVICE 0x01
586 1.21.2.3 bouyer
587 1.21.2.3 bouyer #define UMASS_MAX_TRANSFER_SIZE MAXBSIZE
588 1.21.2.3 bouyer
589 1.21.2.3 bouyer struct scsipi_periphsw umass_probe_periphsw =
590 1.21.2.3 bouyer {
591 1.21.2.3 bouyer NULL,
592 1.21.2.3 bouyer NULL,
593 1.21.2.3 bouyer NULL,
594 1.21.2.3 bouyer NULL,
595 1.21.2.3 bouyer };
596 1.21.2.3 bouyer
597 1.21.2.3 bouyer Static void umass_scsipi_request(struct scsipi_channel *,
598 1.21.2.3 bouyer scsipi_adapter_req_t, void *);
599 1.21.2.3 bouyer Static void umass_scsipi_minphys(struct buf *bp);
600 1.21.2.3 bouyer Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
601 1.21.2.3 bouyer caddr_t, int, struct proc *);
602 1.21.2.3 bouyer Static int umass_scsipi_getgeom(struct scsipi_periph *link,
603 1.21.2.3 bouyer struct disk_parms *, u_long sectors);
604 1.21.2.3 bouyer
605 1.21.2.3 bouyer Static void umass_scsipi_cb(struct umass_softc *sc, void *priv,
606 1.21.2.3 bouyer int residue, int status);
607 1.21.2.3 bouyer Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv,
608 1.21.2.3 bouyer int residue, int status);
609 1.21.2.3 bouyer
610 1.21.2.3 bouyer Static int scsipiprint(void *aux, const char *pnp);
611 1.21.2.3 bouyer Static int umass_ufi_transform(struct umass_softc *sc,
612 1.21.2.3 bouyer struct scsipi_generic *cmd, int cmdlen,
613 1.21.2.3 bouyer struct scsipi_generic *rcmd, int *rcmdlen);
614 1.21.2.3 bouyer #if NATAPIBUS > 0
615 1.21.2.3 bouyer Static void umass_atapi_probe_device(struct atapibus_softc *, int);
616 1.21.2.3 bouyer
617 1.21.2.3 bouyer const struct scsipi_bustype umass_atapi_bustype = {
618 1.21.2.3 bouyer SCSIPI_BUSTYPE_ATAPI,
619 1.21.2.3 bouyer atapi_scsipi_cmd,
620 1.21.2.3 bouyer atapi_interpret_sense,
621 1.21.2.3 bouyer atapi_print_addr,
622 1.21.2.3 bouyer scsi_kill_pending,
623 1.21.2.3 bouyer };
624 1.21.2.3 bouyer
625 1.21.2.3 bouyer #endif
626 1.21.2.3 bouyer #endif
627 1.21.2.3 bouyer
628 1.21.2.3 bouyer #if defined(__FreeBSD__)
629 1.21.2.3 bouyer /* SCSI specific functions */
630 1.21.2.3 bouyer Static int umass_scsi_transform(struct umass_softc *sc,
631 1.21.2.3 bouyer unsigned char *cmd, int cmdlen,
632 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen);
633 1.21.2.3 bouyer
634 1.21.2.3 bouyer /* UFI specific functions */
635 1.21.2.3 bouyer Static int umass_ufi_transform(struct umass_softc *sc,
636 1.21.2.3 bouyer unsigned char *cmd, int cmdlen,
637 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen);
638 1.21.2.3 bouyer
639 1.21.2.3 bouyer /* 8070 specific functions */
640 1.21.2.3 bouyer Static int umass_8070_transform(struct umass_softc *sc,
641 1.21.2.3 bouyer unsigned char *cmd, int cmdlen,
642 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen);
643 1.21.2.3 bouyer #endif
644 1.21.2.3 bouyer
645 1.21.2.3 bouyer #ifdef UMASS_DEBUG
646 1.21.2.3 bouyer /* General debugging functions */
647 1.21.2.3 bouyer Static void umass_bbb_dump_cbw(struct umass_softc *sc,
648 1.21.2.3 bouyer umass_bbb_cbw_t *cbw);
649 1.21.2.3 bouyer Static void umass_bbb_dump_csw(struct umass_softc *sc,
650 1.21.2.3 bouyer umass_bbb_csw_t *csw);
651 1.21.2.3 bouyer Static void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer,
652 1.21.2.3 bouyer int buflen, int printlen);
653 1.21.2.3 bouyer #endif
654 1.21.2.3 bouyer
655 1.21.2.3 bouyer
656 1.21.2.3 bouyer void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
657 1.21.2.3 bouyer
658 1.21.2.3 bouyer /*
659 1.21.2.3 bouyer * USB device probe/attach/detach
660 1.21.2.3 bouyer */
661 1.21.2.3 bouyer
662 1.21.2.3 bouyer /*
663 1.21.2.3 bouyer * Match the device we are seeing with the devices supported. Fill in the
664 1.21.2.3 bouyer * proto and drive fields in the softc accordingly.
665 1.21.2.3 bouyer * This function is called from both probe and attach.
666 1.21.2.3 bouyer */
667 1.21.2.3 bouyer
668 1.21.2.3 bouyer Static int
669 1.21.2.3 bouyer umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
670 1.21.2.3 bouyer usbd_device_handle dev)
671 1.21.2.3 bouyer {
672 1.21.2.3 bouyer usb_device_descriptor_t *dd;
673 1.21.2.3 bouyer usb_interface_descriptor_t *id;
674 1.21.2.4 bouyer u_int vendor, product;
675 1.21.2.3 bouyer
676 1.21.2.3 bouyer /*
677 1.21.2.3 bouyer * Fill in sc->drive and sc->proto and return a match
678 1.21.2.3 bouyer * value if both are determined and 0 otherwise.
679 1.21.2.3 bouyer */
680 1.21.2.3 bouyer
681 1.21.2.3 bouyer sc->drive = DRIVE_GENERIC;
682 1.21.2.3 bouyer sc->proto = PROTO_UNKNOWN;
683 1.21.2.3 bouyer sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED;
684 1.21.2.3 bouyer
685 1.21.2.3 bouyer sc->sc_udev = dev;
686 1.21.2.3 bouyer dd = usbd_get_device_descriptor(dev);
687 1.21.2.4 bouyer vendor = UGETW(dd->idVendor);
688 1.21.2.4 bouyer product = UGETW(dd->idProduct);
689 1.21.2.3 bouyer
690 1.21.2.4 bouyer if (vendor == USB_VENDOR_SHUTTLE &&
691 1.21.2.4 bouyer product == USB_PRODUCT_SHUTTLE_EUSB) {
692 1.21.2.3 bouyer sc->drive = SHUTTLE_EUSB;
693 1.21.2.3 bouyer #if CBI_I
694 1.21.2.3 bouyer sc->proto = PROTO_ATAPI | PROTO_CBI_I;
695 1.21.2.3 bouyer #else
696 1.21.2.3 bouyer sc->proto = PROTO_ATAPI | PROTO_CBI;
697 1.21.2.3 bouyer #endif
698 1.21.2.3 bouyer sc->subclass = UISUBCLASS_SFF8020I;
699 1.21.2.3 bouyer sc->protocol = UIPROTO_MASS_CBI;
700 1.21.2.3 bouyer sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP;
701 1.21.2.3 bouyer return (UMATCH_VENDOR_PRODUCT);
702 1.21.2.3 bouyer }
703 1.21.2.3 bouyer
704 1.21.2.4 bouyer if (vendor == USB_VENDOR_YEDATA &&
705 1.21.2.4 bouyer product == USB_PRODUCT_YEDATA_FLASHBUSTERU) {
706 1.21.2.3 bouyer
707 1.21.2.3 bouyer /* Revisions < 1.28 do not handle the interrupt endpoint
708 1.21.2.3 bouyer * very well.
709 1.21.2.3 bouyer */
710 1.21.2.3 bouyer if (UGETW(dd->bcdDevice) < 0x128)
711 1.21.2.3 bouyer sc->proto = PROTO_UFI | PROTO_CBI;
712 1.21.2.3 bouyer else
713 1.21.2.3 bouyer #if CBI_I
714 1.21.2.3 bouyer sc->proto = PROTO_UFI | PROTO_CBI_I;
715 1.21.2.3 bouyer #else
716 1.21.2.3 bouyer sc->proto = PROTO_UFI | PROTO_CBI;
717 1.21.2.3 bouyer #endif
718 1.21.2.3 bouyer /*
719 1.21.2.3 bouyer * Revisions < 1.28 do not have the TEST UNIT READY command
720 1.21.2.3 bouyer * Revisions == 1.28 have a broken TEST UNIT READY
721 1.21.2.3 bouyer */
722 1.21.2.3 bouyer if (UGETW(dd->bcdDevice) <= 0x128)
723 1.21.2.3 bouyer sc->quirks |= NO_TEST_UNIT_READY;
724 1.21.2.3 bouyer
725 1.21.2.3 bouyer sc->subclass = UISUBCLASS_UFI;
726 1.21.2.3 bouyer sc->protocol = UIPROTO_MASS_CBI;
727 1.21.2.3 bouyer
728 1.21.2.3 bouyer sc->quirks |= RS_NO_CLEAR_UA;
729 1.21.2.3 bouyer sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
730 1.21.2.3 bouyer return (UMATCH_VENDOR_PRODUCT_REV);
731 1.21.2.3 bouyer }
732 1.21.2.3 bouyer
733 1.21.2.4 bouyer if (vendor == USB_VENDOR_INSYSTEM &&
734 1.21.2.4 bouyer product == USB_PRODUCT_INSYSTEM_USBCABLE) {
735 1.21.2.3 bouyer sc->drive = INSYSTEM_USBCABLE;
736 1.21.2.3 bouyer sc->proto = PROTO_ATAPI | PROTO_CBI;
737 1.21.2.3 bouyer sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP;
738 1.21.2.4 bouyer return (UMATCH_VENDOR_PRODUCT);
739 1.21.2.3 bouyer }
740 1.21.2.3 bouyer
741 1.21.2.3 bouyer id = usbd_get_interface_descriptor(iface);
742 1.21.2.3 bouyer if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
743 1.21.2.3 bouyer return (UMATCH_NONE);
744 1.21.2.3 bouyer
745 1.21.2.4 bouyer if (vendor == USB_VENDOR_SONY && id->bInterfaceSubClass == 0xff) {
746 1.21.2.3 bouyer /*
747 1.21.2.3 bouyer * Sony DSC devices set the sub class to 0xff
748 1.21.2.3 bouyer * instead of 1 (RBC). Fix that here.
749 1.21.2.3 bouyer */
750 1.21.2.3 bouyer id->bInterfaceSubClass = UISUBCLASS_RBC;
751 1.21.2.3 bouyer /* They also should be able to do higher speed. */
752 1.21.2.3 bouyer sc->transfer_speed = 500;
753 1.21.2.3 bouyer }
754 1.21.2.4 bouyer
755 1.21.2.4 bouyer if (vendor == USB_VENDOR_FUJIPHOTO &&
756 1.21.2.4 bouyer product == USB_PRODUCT_FUJIPHOTO_MASS0100)
757 1.21.2.4 bouyer sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP;
758 1.21.2.3 bouyer
759 1.21.2.3 bouyer sc->subclass = id->bInterfaceSubClass;
760 1.21.2.3 bouyer sc->protocol = id->bInterfaceProtocol;
761 1.1 thorpej
762 1.21.2.3 bouyer switch (sc->subclass) {
763 1.21.2.3 bouyer case UISUBCLASS_SCSI:
764 1.21.2.3 bouyer sc->proto |= PROTO_SCSI;
765 1.21.2.3 bouyer break;
766 1.21.2.3 bouyer case UISUBCLASS_UFI:
767 1.21.2.3 bouyer sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
768 1.21.2.3 bouyer sc->proto |= PROTO_UFI;
769 1.21.2.3 bouyer break;
770 1.21.2.3 bouyer case UISUBCLASS_SFF8020I:
771 1.21.2.3 bouyer case UISUBCLASS_SFF8070I:
772 1.21.2.3 bouyer case UISUBCLASS_QIC157:
773 1.21.2.3 bouyer sc->proto |= PROTO_ATAPI;
774 1.21.2.3 bouyer break;
775 1.21.2.3 bouyer case UISUBCLASS_RBC:
776 1.21.2.3 bouyer sc->proto |= PROTO_RBC;
777 1.21.2.3 bouyer break;
778 1.21.2.3 bouyer default:
779 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
780 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), id->bInterfaceSubClass));
781 1.21.2.3 bouyer return (UMATCH_NONE);
782 1.21.2.3 bouyer }
783 1.1 thorpej
784 1.21.2.3 bouyer switch (sc->protocol) {
785 1.21.2.3 bouyer case UIPROTO_MASS_CBI:
786 1.21.2.3 bouyer sc->proto |= PROTO_CBI;
787 1.21.2.3 bouyer break;
788 1.21.2.3 bouyer case UIPROTO_MASS_CBI_I:
789 1.21.2.3 bouyer #if CBI_I
790 1.21.2.3 bouyer sc->proto |= PROTO_CBI_I;
791 1.21.2.3 bouyer #else
792 1.21.2.3 bouyer sc->proto |= PROTO_CBI;
793 1.21.2.3 bouyer #endif
794 1.21.2.3 bouyer break;
795 1.21.2.3 bouyer case UIPROTO_MASS_BBB:
796 1.21.2.3 bouyer sc->proto |= PROTO_BBB;
797 1.21.2.3 bouyer break;
798 1.21.2.3 bouyer case UIPROTO_MASS_BBB_P:
799 1.21.2.3 bouyer sc->drive = ZIP_100;
800 1.21.2.3 bouyer sc->proto |= PROTO_BBB;
801 1.21.2.3 bouyer sc->transfer_speed = UMASS_ZIP100_TRANSFER_SPEED;
802 1.21.2.3 bouyer sc->quirks |= NO_TEST_UNIT_READY;
803 1.21.2.3 bouyer break;
804 1.21.2.3 bouyer default:
805 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
806 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), id->bInterfaceProtocol));
807 1.21.2.3 bouyer return (UMATCH_NONE);
808 1.21.2.3 bouyer }
809 1.1 thorpej
810 1.21.2.3 bouyer return (UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
811 1.21.2.3 bouyer }
812 1.1 thorpej
813 1.1 thorpej USB_MATCH(umass)
814 1.1 thorpej {
815 1.1 thorpej USB_MATCH_START(umass, uaa);
816 1.21.2.3 bouyer #if defined(__FreeBSD__)
817 1.21.2.3 bouyer struct umass_softc *sc = device_get_softc(self);
818 1.21.2.3 bouyer #else if defined(__NetBSD__) || defined(__OpenBSD__)
819 1.21.2.3 bouyer struct umass_softc scs, *sc = &scs;
820 1.21.2.3 bouyer memset(sc, 0, sizeof *sc);
821 1.21.2.3 bouyer strcpy(sc->sc_dev.dv_xname, "umass");
822 1.21.2.3 bouyer #endif
823 1.1 thorpej
824 1.21.2.3 bouyer if (uaa->iface == NULL)
825 1.1 thorpej return(UMATCH_NONE);
826 1.1 thorpej
827 1.21.2.3 bouyer return (umass_match_proto(sc, uaa->iface, uaa->device));
828 1.1 thorpej }
829 1.1 thorpej
830 1.1 thorpej USB_ATTACH(umass)
831 1.1 thorpej {
832 1.1 thorpej USB_ATTACH_START(umass, sc, uaa);
833 1.1 thorpej usb_interface_descriptor_t *id;
834 1.1 thorpej usb_endpoint_descriptor_t *ed;
835 1.21.2.3 bouyer const char *sSubclass, *sProto;
836 1.1 thorpej char devinfo[1024];
837 1.21.2.3 bouyer int i, bno;
838 1.21.2.3 bouyer int err;
839 1.1 thorpej
840 1.21.2.3 bouyer /*
841 1.21.2.3 bouyer * the softc struct is bzero-ed in device_set_driver. We can safely
842 1.21.2.3 bouyer * call umass_detach without specifically initialising the struct.
843 1.21.2.3 bouyer */
844 1.1 thorpej
845 1.1 thorpej usbd_devinfo(uaa->device, 0, devinfo);
846 1.1 thorpej USB_ATTACH_SETUP;
847 1.1 thorpej
848 1.21.2.3 bouyer sc->iface = uaa->iface;
849 1.21.2.3 bouyer sc->ifaceno = uaa->ifaceno;
850 1.17 thorpej
851 1.21.2.3 bouyer /* initialise the proto and drive values in the umass_softc (again) */
852 1.21.2.3 bouyer if (umass_match_proto(sc, sc->iface, uaa->device) == 0) {
853 1.21.2.3 bouyer printf("%s: match failed\n", USBDEVNAME(sc->sc_dev));
854 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
855 1.21.2.3 bouyer }
856 1.17 thorpej
857 1.21.2.3 bouyer /*
858 1.21.2.3 bouyer * The timeout is based on the maximum expected transfer size
859 1.21.2.3 bouyer * divided by the expected transfer speed.
860 1.21.2.3 bouyer * We multiply by 4 to make sure a busy system doesn't make things
861 1.21.2.3 bouyer * fail.
862 1.21.2.3 bouyer */
863 1.21.2.3 bouyer sc->timeout = 4 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed;
864 1.21.2.3 bouyer sc->timeout += UMASS_SPINUP_TIME; /* allow for spinning up */
865 1.21.2.3 bouyer
866 1.21.2.3 bouyer id = usbd_get_interface_descriptor(sc->iface);
867 1.21.2.3 bouyer printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
868 1.21.2.3 bouyer
869 1.21.2.3 bouyer switch (sc->subclass) {
870 1.21.2.3 bouyer case UISUBCLASS_RBC:
871 1.21.2.3 bouyer sSubclass = "RBC";
872 1.21.2.3 bouyer break;
873 1.21.2.3 bouyer case UISUBCLASS_SCSI:
874 1.21.2.3 bouyer sSubclass = "SCSI";
875 1.21.2.3 bouyer break;
876 1.21.2.3 bouyer case UISUBCLASS_UFI:
877 1.21.2.3 bouyer sSubclass = "UFI";
878 1.21.2.3 bouyer break;
879 1.21.2.3 bouyer case UISUBCLASS_SFF8020I:
880 1.21.2.3 bouyer sSubclass = "SFF8020i";
881 1.21.2.3 bouyer break;
882 1.21.2.3 bouyer case UISUBCLASS_SFF8070I:
883 1.21.2.3 bouyer sSubclass = "SFF8070i";
884 1.21.2.3 bouyer break;
885 1.21.2.3 bouyer case UISUBCLASS_QIC157:
886 1.21.2.3 bouyer sSubclass = "QIC157";
887 1.21.2.3 bouyer break;
888 1.17 thorpej default:
889 1.21.2.3 bouyer sSubclass = "unknown";
890 1.21.2.3 bouyer break;
891 1.17 thorpej }
892 1.21.2.3 bouyer switch (sc->protocol) {
893 1.21.2.3 bouyer case UIPROTO_MASS_CBI:
894 1.21.2.3 bouyer sProto = "CBI";
895 1.21.2.3 bouyer break;
896 1.21.2.3 bouyer case UIPROTO_MASS_CBI_I:
897 1.21.2.3 bouyer sProto = "CBI-I";
898 1.21.2.3 bouyer break;
899 1.21.2.3 bouyer case UIPROTO_MASS_BBB:
900 1.21.2.3 bouyer sProto = "BBB";
901 1.21.2.3 bouyer break;
902 1.21.2.3 bouyer case UIPROTO_MASS_BBB_P:
903 1.21.2.3 bouyer sProto = "BBB-P";
904 1.21.2.3 bouyer break;
905 1.17 thorpej default:
906 1.21.2.3 bouyer sProto = "unknown";
907 1.21.2.3 bouyer break;
908 1.17 thorpej }
909 1.21.2.3 bouyer printf("%s: using %s over %s\n", USBDEVNAME(sc->sc_dev), sSubclass,
910 1.21.2.3 bouyer sProto);
911 1.17 thorpej
912 1.21.2.3 bouyer if (sc->drive == INSYSTEM_USBCABLE) {
913 1.21.2.3 bouyer err = usbd_set_interface(0, 1);
914 1.21.2.3 bouyer if (err) {
915 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: could not switch to "
916 1.21.2.3 bouyer "Alt Interface %d\n",
917 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), 1));
918 1.21.2.3 bouyer umass_disco(sc);
919 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
920 1.21.2.3 bouyer }
921 1.21.2.3 bouyer }
922 1.1 thorpej
923 1.1 thorpej /*
924 1.21.2.3 bouyer * In addition to the Control endpoint the following endpoints
925 1.21.2.3 bouyer * are required:
926 1.21.2.3 bouyer * a) bulk-in endpoint.
927 1.21.2.3 bouyer * b) bulk-out endpoint.
928 1.21.2.3 bouyer * and for Control/Bulk/Interrupt with CCI (CBI_I)
929 1.21.2.3 bouyer * c) intr-in
930 1.1 thorpej *
931 1.1 thorpej * The endpoint addresses are not fixed, so we have to read them
932 1.1 thorpej * from the device descriptors of the current interface.
933 1.1 thorpej */
934 1.1 thorpej for (i = 0 ; i < id->bNumEndpoints ; i++) {
935 1.21.2.3 bouyer ed = usbd_interface2endpoint_descriptor(sc->iface, i);
936 1.1 thorpej if (!ed) {
937 1.1 thorpej printf("%s: could not read endpoint descriptor\n",
938 1.1 thorpej USBDEVNAME(sc->sc_dev));
939 1.1 thorpej USB_ATTACH_ERROR_RETURN;
940 1.1 thorpej }
941 1.11 augustss if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
942 1.1 thorpej && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
943 1.21.2.3 bouyer sc->bulkin = ed->bEndpointAddress;
944 1.11 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
945 1.1 thorpej && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
946 1.21.2.3 bouyer sc->bulkout = ed->bEndpointAddress;
947 1.21.2.3 bouyer } else if (sc->proto & PROTO_CBI_I
948 1.21.2.3 bouyer && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
949 1.21.2.3 bouyer && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
950 1.21.2.3 bouyer sc->intrin = ed->bEndpointAddress;
951 1.21.2.3 bouyer #ifdef UMASS_DEBUG
952 1.21.2.3 bouyer if (UGETW(ed->wMaxPacketSize) > 2) {
953 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
954 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
955 1.21.2.3 bouyer UGETW(ed->wMaxPacketSize)));
956 1.21.2.3 bouyer }
957 1.21.2.3 bouyer #endif
958 1.1 thorpej }
959 1.1 thorpej }
960 1.1 thorpej
961 1.21.2.3 bouyer /* check whether we found all the endpoints we need */
962 1.21.2.3 bouyer if (!sc->bulkin || !sc->bulkout
963 1.21.2.3 bouyer || (sc->proto & PROTO_CBI_I && !sc->intrin) ) {
964 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
965 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
966 1.21.2.3 bouyer sc->bulkin, sc->bulkout, sc->intrin));
967 1.21.2.3 bouyer umass_disco(sc);
968 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
969 1.21.2.3 bouyer }
970 1.21.2.3 bouyer
971 1.7 thorpej /*
972 1.7 thorpej * Get the maximum LUN supported by the device.
973 1.7 thorpej */
974 1.21.2.3 bouyer if ((sc->proto & PROTO_WIRE) == PROTO_BBB) {
975 1.21.2.3 bouyer err = umass_bbb_get_max_lun(sc, &sc->maxlun);
976 1.21.2.3 bouyer if (err) {
977 1.21.2.3 bouyer printf("%s: unable to get Max Lun: %s\n",
978 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
979 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
980 1.21.2.3 bouyer }
981 1.21.2.3 bouyer } else {
982 1.21.2.3 bouyer sc->maxlun = 0;
983 1.7 thorpej }
984 1.7 thorpej
985 1.1 thorpej /* Open the bulk-in and -out pipe */
986 1.21.2.3 bouyer err = usbd_open_pipe(sc->iface, sc->bulkout,
987 1.21.2.3 bouyer USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
988 1.1 thorpej if (err) {
989 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
990 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->bulkout));
991 1.21.2.3 bouyer umass_disco(sc);
992 1.1 thorpej USB_ATTACH_ERROR_RETURN;
993 1.1 thorpej }
994 1.21.2.3 bouyer err = usbd_open_pipe(sc->iface, sc->bulkin,
995 1.21.2.3 bouyer USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
996 1.1 thorpej if (err) {
997 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
998 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->bulkin));
999 1.21.2.3 bouyer umass_disco(sc);
1000 1.1 thorpej USB_ATTACH_ERROR_RETURN;
1001 1.1 thorpej }
1002 1.21.2.3 bouyer /*
1003 1.21.2.3 bouyer * Open the intr-in pipe if the protocol is CBI with CCI.
1004 1.21.2.3 bouyer * Note: early versions of the Zip drive do have an interrupt pipe, but
1005 1.21.2.3 bouyer * this pipe is unused
1006 1.21.2.3 bouyer *
1007 1.21.2.3 bouyer * We do not open the interrupt pipe as an interrupt pipe, but as a
1008 1.21.2.3 bouyer * normal bulk endpoint. We send an IN transfer down the wire at the
1009 1.21.2.3 bouyer * appropriate time, because we know exactly when to expect data on
1010 1.21.2.3 bouyer * that endpoint. This saves bandwidth, but more important, makes the
1011 1.21.2.3 bouyer * code for handling the data on that endpoint simpler. No data
1012 1.21.2.3 bouyer * arriving concurrently.
1013 1.21.2.3 bouyer */
1014 1.21.2.3 bouyer if (sc->proto & PROTO_CBI_I) {
1015 1.21.2.3 bouyer err = usbd_open_pipe(sc->iface, sc->intrin,
1016 1.21.2.3 bouyer USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1017 1.21.2.3 bouyer if (err) {
1018 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1019 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->intrin));
1020 1.21.2.3 bouyer umass_disco(sc);
1021 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
1022 1.21.2.3 bouyer }
1023 1.21.2.3 bouyer }
1024 1.21.2.3 bouyer
1025 1.21.2.3 bouyer /* initialisation of generic part */
1026 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
1027 1.21.2.3 bouyer
1028 1.21.2.3 bouyer /* request a sufficient number of xfer handles */
1029 1.21.2.3 bouyer for (i = 0; i < XFER_NR; i++) {
1030 1.21.2.3 bouyer sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1031 1.21.2.3 bouyer if (sc->transfer_xfer[i] == 0) {
1032 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1033 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1034 1.21.2.3 bouyer umass_disco(sc);
1035 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
1036 1.21.2.3 bouyer }
1037 1.21.2.3 bouyer }
1038 1.21.2.3 bouyer /* Allocate buffer for data transfer (it's huge). */
1039 1.21.2.3 bouyer switch (sc->proto & PROTO_WIRE) {
1040 1.21.2.3 bouyer case PROTO_BBB:
1041 1.21.2.3 bouyer bno = XFER_BBB_DATA;
1042 1.21.2.3 bouyer goto dalloc;
1043 1.21.2.3 bouyer case PROTO_CBI:
1044 1.21.2.3 bouyer bno = XFER_CBI_DATA;
1045 1.21.2.3 bouyer goto dalloc;
1046 1.21.2.3 bouyer case PROTO_CBI_I:
1047 1.21.2.3 bouyer bno = XFER_CBI_DATA;
1048 1.21.2.3 bouyer dalloc:
1049 1.21.2.3 bouyer sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
1050 1.21.2.3 bouyer UMASS_MAX_TRANSFER_SIZE);
1051 1.21.2.3 bouyer if (sc->data_buffer == NULL) {
1052 1.21.2.3 bouyer umass_disco(sc);
1053 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
1054 1.21.2.3 bouyer }
1055 1.21.2.3 bouyer break;
1056 1.21.2.3 bouyer default:
1057 1.21.2.3 bouyer break;
1058 1.21.2.3 bouyer }
1059 1.21.2.3 bouyer
1060 1.21.2.3 bouyer /* Initialise the wire protocol specific methods */
1061 1.21.2.3 bouyer if (sc->proto & PROTO_BBB) {
1062 1.21.2.3 bouyer sc->reset = umass_bbb_reset;
1063 1.21.2.3 bouyer sc->transfer = umass_bbb_transfer;
1064 1.21.2.3 bouyer sc->state = umass_bbb_state;
1065 1.21.2.3 bouyer } else if ((sc->proto & PROTO_CBI) || (sc->proto & PROTO_CBI_I)) {
1066 1.21.2.3 bouyer sc->reset = umass_cbi_reset;
1067 1.21.2.3 bouyer sc->transfer = umass_cbi_transfer;
1068 1.21.2.3 bouyer sc->state = umass_cbi_state;
1069 1.21.2.3 bouyer #ifdef UMASS_DEBUG
1070 1.21.2.3 bouyer } else {
1071 1.21.2.3 bouyer panic("%s:%d: Unknown proto 0x%02x\n",
1072 1.21.2.3 bouyer __FILE__, __LINE__, sc->proto);
1073 1.21.2.3 bouyer #endif
1074 1.21.2.3 bouyer }
1075 1.21.2.3 bouyer
1076 1.21.2.3 bouyer if (sc->drive == SHUTTLE_EUSB)
1077 1.21.2.3 bouyer umass_init_shuttle(sc);
1078 1.21.2.3 bouyer
1079 1.21.2.3 bouyer #if defined(__FreeBSD__)
1080 1.21.2.3 bouyer if (sc->proto & PROTO_SCSI)
1081 1.21.2.3 bouyer sc->transform = umass_scsi_transform;
1082 1.21.2.3 bouyer else if (sc->proto & PROTO_UFI)
1083 1.21.2.3 bouyer sc->transform = umass_ufi_transform;
1084 1.21.2.3 bouyer else if (sc->proto & PROTO_ATAPI)
1085 1.21.2.3 bouyer sc->transform = umass_8070_transform;
1086 1.21.2.3 bouyer #ifdef UMASS_DEBUG
1087 1.21.2.3 bouyer else
1088 1.21.2.3 bouyer panic("No transformation defined for command proto 0x%02x\n",
1089 1.21.2.3 bouyer sc->proto & PROTO_COMMAND);
1090 1.21.2.3 bouyer #endif
1091 1.21.2.3 bouyer
1092 1.21.2.3 bouyer /* From here onwards the device can be used. */
1093 1.21.2.3 bouyer
1094 1.21.2.3 bouyer if ((sc->proto & PROTO_SCSI) ||
1095 1.21.2.3 bouyer (sc->proto & PROTO_ATAPI) ||
1096 1.21.2.3 bouyer (sc->proto & PROTO_UFI)) {
1097 1.21.2.3 bouyer /* Prepare the SCSI command block */
1098 1.21.2.3 bouyer sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1099 1.21.2.3 bouyer
1100 1.21.2.3 bouyer /* If this is the first device register the SIM */
1101 1.21.2.3 bouyer if (umass_sim == NULL) {
1102 1.21.2.3 bouyer err = umass_cam_attach_sim();
1103 1.21.2.3 bouyer if (err) {
1104 1.21.2.3 bouyer umass_disco(self);
1105 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
1106 1.21.2.3 bouyer }
1107 1.21.2.3 bouyer }
1108 1.1 thorpej
1109 1.21.2.3 bouyer /* Attach the new device to our SCSI host controller (SIM) */
1110 1.21.2.3 bouyer err = umass_cam_attach(sc);
1111 1.21.2.3 bouyer if (err) {
1112 1.21.2.3 bouyer umass_disco(self);
1113 1.21.2.3 bouyer USB_ATTACH_ERROR_RETURN;
1114 1.21.2.3 bouyer }
1115 1.21.2.3 bouyer } else {
1116 1.21.2.3 bouyer panic("%s:%d: Unknown proto 0x%02x\n",
1117 1.21.2.3 bouyer __FILE__, __LINE__, sc->proto);
1118 1.21.2.3 bouyer }
1119 1.21.2.3 bouyer #elif defined(__NetBSD__) || defined(__OpenBSD__)
1120 1.21.2.3 bouyer /*
1121 1.21.2.3 bouyer * Fill in the adapter.
1122 1.21.2.3 bouyer */
1123 1.21.2.3 bouyer memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
1124 1.21.2.1 thorpej sc->sc_adapter.adapt_dev = &sc->sc_dev;
1125 1.21.2.1 thorpej sc->sc_adapter.adapt_nchannels = 1;
1126 1.21.2.1 thorpej sc->sc_adapter.adapt_request = umass_scsipi_request;
1127 1.21.2.1 thorpej sc->sc_adapter.adapt_minphys = umass_scsipi_minphys;
1128 1.21.2.3 bouyer sc->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
1129 1.21.2.3 bouyer sc->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
1130 1.21.2.3 bouyer sc->sc_atapi_adapter.atapi_probe_device = umass_atapi_probe_device;
1131 1.21.2.1 thorpej
1132 1.21.2.3 bouyer /* fill in the channel */
1133 1.21.2.3 bouyer memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
1134 1.21.2.1 thorpej sc->sc_channel.chan_adapter = &sc->sc_adapter;
1135 1.21.2.3 bouyer sc->sc_channel.chan_channel = 1;
1136 1.21.2.3 bouyer sc->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS;
1137 1.21.2.3 bouyer sc->sc_channel.chan_openings = 1;
1138 1.21.2.3 bouyer sc->sc_channel.chan_max_periph = 1;
1139 1.21.2.3 bouyer
1140 1.21.2.3 bouyer switch (sc->proto & PROTO_COMMAND) {
1141 1.21.2.3 bouyer case PROTO_UFI:
1142 1.21.2.3 bouyer case PROTO_RBC:
1143 1.21.2.3 bouyer sc->sc_channel.chan_defquirks |= PQUIRK_ONLYBIG;
1144 1.21.2.3 bouyer /* fall into */
1145 1.21.2.3 bouyer case PROTO_SCSI:
1146 1.21.2.3 bouyer if (sc->quirks & NO_TEST_UNIT_READY)
1147 1.21.2.3 bouyer sc->sc_channel.chan_defquirks |= PQUIRK_NOTUR;
1148 1.21.2.3 bouyer
1149 1.21.2.3 bouyer sc->sc_channel.chan_bustype = &scsi_bustype;
1150 1.21.2.3 bouyer sc->sc_channel.chan_ntargets = UMASS_SCSIID_DEVICE + 1;
1151 1.21.2.3 bouyer sc->sc_channel.chan_nluns = sc->maxlun + 1;
1152 1.21.2.3 bouyer sc->sc_channel.chan_id = UMASS_SCSIID_HOST;
1153 1.21.2.3 bouyer sc->sc_channel.type = BUS_SCSI;
1154 1.21.2.3 bouyer sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsipiprint);
1155 1.21.2.3 bouyer break;
1156 1.1 thorpej
1157 1.21.2.3 bouyer #if NATAPIBUS > 0
1158 1.21.2.3 bouyer case PROTO_ATAPI:
1159 1.21.2.3 bouyer sc->sc_channel.chan_bustype = &umass_atapi_bustype;
1160 1.21.2.3 bouyer sc->sc_channel.chan_ntargets = 2;
1161 1.21.2.3 bouyer sc->sc_channel.chan_nluns = 1;
1162 1.21.2.3 bouyer
1163 1.21.2.3 bouyer sc->aa.sc_aa.aa_type = T_ATAPI;
1164 1.21.2.3 bouyer sc->aa.sc_aa.aa_channel = 0;
1165 1.21.2.3 bouyer sc->aa.sc_aa.aa_openings = 1;
1166 1.21.2.3 bouyer sc->aa.sc_aa.aa_drv_data = &sc->aa.sc_aa_drive;
1167 1.21.2.3 bouyer sc->aa.sc_aa.aa_bus_private = &sc->sc_atapi_adapter;
1168 1.21.2.3 bouyer sc->sc_child = config_found(&sc->sc_dev, &sc->aa, scsipiprint);
1169 1.21.2.3 bouyer break;
1170 1.21.2.3 bouyer #endif
1171 1.21.2.3 bouyer
1172 1.21.2.3 bouyer default:
1173 1.21.2.3 bouyer printf("%s: proto=0x%x not supported yet\n",
1174 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->proto);
1175 1.21.2.3 bouyer umass_disco(sc);
1176 1.1 thorpej USB_ATTACH_ERROR_RETURN;
1177 1.1 thorpej }
1178 1.1 thorpej
1179 1.21.2.3 bouyer if (sc->sc_child == NULL) {
1180 1.21.2.3 bouyer umass_disco(sc);
1181 1.21.2.3 bouyer /* Not an error, just not a complete success. */
1182 1.21.2.3 bouyer USB_ATTACH_SUCCESS_RETURN;
1183 1.21.2.3 bouyer }
1184 1.21.2.3 bouyer #endif
1185 1.21.2.3 bouyer
1186 1.21.2.3 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
1187 1.21.2.3 bouyer USBDEV(sc->sc_dev));
1188 1.21.2.3 bouyer
1189 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
1190 1.21.2.3 bouyer
1191 1.1 thorpej USB_ATTACH_SUCCESS_RETURN;
1192 1.1 thorpej }
1193 1.1 thorpej
1194 1.21.2.3 bouyer Static int
1195 1.21.2.3 bouyer scsipiprint(void *aux, const char *pnp)
1196 1.21.2.3 bouyer {
1197 1.21.2.3 bouyer struct scsipi_channel *chan = aux;
1198 1.21.2.3 bouyer
1199 1.21.2.3 bouyer if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_SCSI)
1200 1.21.2.3 bouyer return (scsiprint(aux, pnp));
1201 1.21.2.3 bouyer else {
1202 1.21.2.3 bouyer #if NATAPIBUS > 0
1203 1.21.2.6 bouyer struct ata_atapi_attach *aa_link = aux;
1204 1.21.2.6 bouyer #endif
1205 1.21.2.3 bouyer if (pnp)
1206 1.21.2.3 bouyer printf("atapibus at %s", pnp);
1207 1.21.2.6 bouyer #if NATAPIBUS > 0
1208 1.21.2.6 bouyer printf(" channel %d", aa_link->aa_channel);
1209 1.21.2.3 bouyer #endif
1210 1.21.2.6 bouyer return (UNCONF);
1211 1.21.2.3 bouyer }
1212 1.21.2.3 bouyer }
1213 1.21.2.3 bouyer
1214 1.21.2.3 bouyer USB_DETACH(umass)
1215 1.21.2.3 bouyer {
1216 1.21.2.3 bouyer USB_DETACH_START(umass, sc);
1217 1.21.2.3 bouyer int rv = 0;
1218 1.21.2.3 bouyer
1219 1.21.2.3 bouyer DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
1220 1.21.2.3 bouyer
1221 1.21.2.3 bouyer /* Abort the pipes to wake up any waiting processes. */
1222 1.21.2.3 bouyer if (sc->bulkout_pipe != NULL)
1223 1.21.2.3 bouyer usbd_abort_pipe(sc->bulkout_pipe);
1224 1.21.2.3 bouyer if (sc->bulkin_pipe != NULL)
1225 1.21.2.3 bouyer usbd_abort_pipe(sc->bulkin_pipe);
1226 1.21.2.3 bouyer if (sc->intrin_pipe != NULL)
1227 1.21.2.3 bouyer usbd_abort_pipe(sc->intrin_pipe);
1228 1.21.2.3 bouyer
1229 1.21.2.3 bouyer #if 0
1230 1.21.2.3 bouyer /* Do we really need reference counting? Perhaps in ioctl() */
1231 1.21.2.3 bouyer s = splusb();
1232 1.21.2.3 bouyer if (--sc->sc_refcnt >= 0) {
1233 1.21.2.3 bouyer /* Wait for processes to go away. */
1234 1.21.2.3 bouyer usb_detach_wait(USBDEV(sc->sc_dev));
1235 1.21.2.3 bouyer }
1236 1.21.2.3 bouyer splx(s);
1237 1.21.2.3 bouyer #endif
1238 1.21.2.3 bouyer
1239 1.21.2.3 bouyer #if defined(__FreeBSD__)
1240 1.21.2.3 bouyer if ((sc->proto & PROTO_SCSI) ||
1241 1.21.2.3 bouyer (sc->proto & PROTO_ATAPI) ||
1242 1.21.2.3 bouyer (sc->proto & PROTO_UFI))
1243 1.21.2.3 bouyer /* detach the device from the SCSI host controller (SIM) */
1244 1.21.2.3 bouyer rv = umass_cam_detach(sc);
1245 1.21.2.3 bouyer #elif defined(__NetBSD__) || defined(__OpenBSD__)
1246 1.21.2.3 bouyer if (sc->sc_child != NULL)
1247 1.21.2.3 bouyer rv = config_detach(sc->sc_child, flags);
1248 1.21.2.3 bouyer #endif
1249 1.21.2.3 bouyer if (rv != 0)
1250 1.21.2.3 bouyer return (rv);
1251 1.21.2.3 bouyer
1252 1.21.2.3 bouyer umass_disco(sc);
1253 1.21.2.3 bouyer
1254 1.21.2.3 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1255 1.21.2.3 bouyer USBDEV(sc->sc_dev));
1256 1.21.2.3 bouyer
1257 1.21.2.3 bouyer return (0);
1258 1.21.2.3 bouyer }
1259 1.21.2.3 bouyer
1260 1.21.2.3 bouyer #if defined(__NetBSD__) || defined(__OpenBSD__)
1261 1.21.2.3 bouyer int
1262 1.21.2.3 bouyer umass_activate(struct device *self, enum devact act)
1263 1.1 thorpej {
1264 1.9 thorpej struct umass_softc *sc = (struct umass_softc *) self;
1265 1.21.2.3 bouyer int rv = 0;
1266 1.9 thorpej
1267 1.9 thorpej DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
1268 1.9 thorpej USBDEVNAME(sc->sc_dev), act));
1269 1.1 thorpej
1270 1.1 thorpej switch (act) {
1271 1.1 thorpej case DVACT_ACTIVATE:
1272 1.9 thorpej rv = EOPNOTSUPP;
1273 1.1 thorpej break;
1274 1.1 thorpej
1275 1.1 thorpej case DVACT_DEACTIVATE:
1276 1.21.2.3 bouyer if (sc->sc_child == NULL)
1277 1.9 thorpej break;
1278 1.9 thorpej rv = config_deactivate(sc->sc_child);
1279 1.9 thorpej DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
1280 1.9 thorpej "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
1281 1.9 thorpej if (rv == 0)
1282 1.9 thorpej sc->sc_dying = 1;
1283 1.1 thorpej break;
1284 1.1 thorpej }
1285 1.9 thorpej return (rv);
1286 1.1 thorpej }
1287 1.21.2.3 bouyer #endif
1288 1.1 thorpej
1289 1.21.2.3 bouyer Static void
1290 1.21.2.3 bouyer umass_disco(struct umass_softc *sc)
1291 1.21.2.3 bouyer {
1292 1.21.2.3 bouyer int i;
1293 1.9 thorpej
1294 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("umass_disco\n"));
1295 1.18 augustss
1296 1.21.2.3 bouyer /* Free the xfers. */
1297 1.21.2.3 bouyer for (i = 0; i < XFER_NR; i++)
1298 1.21.2.3 bouyer if (sc->transfer_xfer[i] != NULL) {
1299 1.21.2.3 bouyer usbd_free_xfer(sc->transfer_xfer[i]);
1300 1.21.2.3 bouyer sc->transfer_xfer[i] = NULL;
1301 1.21.2.3 bouyer }
1302 1.18 augustss
1303 1.21.2.3 bouyer /* Remove all the pipes. */
1304 1.21.2.3 bouyer if (sc->bulkout_pipe != NULL)
1305 1.21.2.3 bouyer usbd_close_pipe(sc->bulkout_pipe);
1306 1.21.2.3 bouyer if (sc->bulkin_pipe != NULL)
1307 1.21.2.3 bouyer usbd_close_pipe(sc->bulkin_pipe);
1308 1.21.2.3 bouyer if (sc->intrin_pipe != NULL)
1309 1.21.2.3 bouyer usbd_close_pipe(sc->intrin_pipe);
1310 1.21.2.3 bouyer }
1311 1.18 augustss
1312 1.21.2.3 bouyer Static void
1313 1.21.2.3 bouyer umass_init_shuttle(struct umass_softc *sc)
1314 1.21.2.3 bouyer {
1315 1.21.2.3 bouyer usb_device_request_t req;
1316 1.21.2.3 bouyer u_char status[2];
1317 1.1 thorpej
1318 1.21.2.3 bouyer /* The Linux driver does this */
1319 1.21.2.3 bouyer req.bmRequestType = UT_READ_VENDOR_DEVICE;
1320 1.21.2.3 bouyer req.bRequest = 1;
1321 1.21.2.3 bouyer USETW(req.wValue, 0);
1322 1.21.2.3 bouyer USETW(req.wIndex, sc->ifaceno);
1323 1.21.2.3 bouyer USETW(req.wLength, sizeof status);
1324 1.21.2.3 bouyer (void)usbd_do_request(sc->sc_udev, &req, &status);
1325 1.1 thorpej }
1326 1.1 thorpej
1327 1.21.2.3 bouyer /*
1328 1.21.2.3 bouyer * Generic functions to handle transfers
1329 1.1 thorpej */
1330 1.1 thorpej
1331 1.21.2.3 bouyer Static usbd_status
1332 1.21.2.3 bouyer umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1333 1.21.2.3 bouyer void *buffer, int buflen, int flags,
1334 1.21.2.3 bouyer usbd_xfer_handle xfer)
1335 1.1 thorpej {
1336 1.1 thorpej usbd_status err;
1337 1.1 thorpej
1338 1.21.2.3 bouyer if (sc->sc_dying)
1339 1.21.2.3 bouyer return (USBD_IOERROR);
1340 1.1 thorpej
1341 1.21.2.3 bouyer /* Initialiase a USB transfer and then schedule it */
1342 1.1 thorpej
1343 1.21.2.3 bouyer usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
1344 1.21.2.3 bouyer flags | sc->sc_xfer_flags, sc->timeout, sc->state);
1345 1.21.2.3 bouyer
1346 1.21.2.3 bouyer err = usbd_transfer(xfer);
1347 1.21.2.3 bouyer DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
1348 1.21.2.3 bouyer "timeout=%d\n", USBDEVNAME(sc->sc_dev),
1349 1.21.2.3 bouyer buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
1350 1.21.2.3 bouyer if (err && err != USBD_IN_PROGRESS) {
1351 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1352 1.15 thorpej USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
1353 1.21.2.3 bouyer return (err);
1354 1.1 thorpej }
1355 1.1 thorpej
1356 1.21.2.3 bouyer return (USBD_NORMAL_COMPLETION);
1357 1.21.2.3 bouyer }
1358 1.21.2.3 bouyer
1359 1.21.2.3 bouyer
1360 1.21.2.3 bouyer Static usbd_status
1361 1.21.2.3 bouyer umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle dev,
1362 1.21.2.3 bouyer usb_device_request_t *req,
1363 1.21.2.3 bouyer void *buffer, int buflen, int flags,
1364 1.21.2.3 bouyer usbd_xfer_handle xfer)
1365 1.21.2.3 bouyer {
1366 1.21.2.3 bouyer usbd_status err;
1367 1.21.2.3 bouyer
1368 1.21.2.3 bouyer if (sc->sc_dying)
1369 1.21.2.3 bouyer return (USBD_IOERROR);
1370 1.21.2.3 bouyer
1371 1.21.2.3 bouyer /* Initialiase a USB control transfer and then schedule it */
1372 1.1 thorpej
1373 1.21.2.3 bouyer usbd_setup_default_xfer(xfer, dev, (void *) sc,
1374 1.21.2.3 bouyer sc->timeout, req, buffer, buflen, flags, sc->state);
1375 1.1 thorpej
1376 1.21.2.3 bouyer err = usbd_transfer(xfer);
1377 1.21.2.3 bouyer if (err && err != USBD_IN_PROGRESS) {
1378 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1379 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
1380 1.21.2.3 bouyer
1381 1.21.2.3 bouyer /* do not reset, as this would make us loop */
1382 1.21.2.3 bouyer return (err);
1383 1.21.2.3 bouyer }
1384 1.21.2.3 bouyer
1385 1.21.2.3 bouyer return (USBD_NORMAL_COMPLETION);
1386 1.1 thorpej }
1387 1.1 thorpej
1388 1.21.2.3 bouyer Static void
1389 1.21.2.3 bouyer umass_clear_endpoint_stall(struct umass_softc *sc,
1390 1.21.2.3 bouyer u_int8_t endpt, usbd_pipe_handle pipe,
1391 1.21.2.3 bouyer int state, usbd_xfer_handle xfer)
1392 1.7 thorpej {
1393 1.7 thorpej usbd_device_handle dev;
1394 1.15 thorpej
1395 1.21.2.3 bouyer if (sc->sc_dying)
1396 1.21.2.3 bouyer return;
1397 1.1 thorpej
1398 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1399 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), endpt));
1400 1.7 thorpej
1401 1.21.2.3 bouyer usbd_interface2device_handle(sc->iface, &dev);
1402 1.7 thorpej
1403 1.21.2.3 bouyer sc->transfer_state = state;
1404 1.7 thorpej
1405 1.21.2.3 bouyer usbd_clear_endpoint_toggle(pipe);
1406 1.7 thorpej
1407 1.21.2.3 bouyer sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1408 1.21.2.3 bouyer sc->request.bRequest = UR_CLEAR_FEATURE;
1409 1.21.2.3 bouyer USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1410 1.21.2.3 bouyer USETW(sc->request.wIndex, endpt);
1411 1.21.2.3 bouyer USETW(sc->request.wLength, 0);
1412 1.21.2.3 bouyer umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, xfer);
1413 1.21.2.3 bouyer }
1414 1.15 thorpej
1415 1.21.2.3 bouyer #if 0
1416 1.21.2.3 bouyer Static void
1417 1.21.2.3 bouyer umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1418 1.21.2.3 bouyer {
1419 1.21.2.3 bouyer sc->transfer_cb = cb;
1420 1.21.2.3 bouyer sc->transfer_priv = priv;
1421 1.1 thorpej
1422 1.21.2.3 bouyer /* The reset is a forced reset, so no error (yet) */
1423 1.21.2.3 bouyer sc->reset(sc, STATUS_CMD_OK);
1424 1.7 thorpej }
1425 1.21.2.3 bouyer #endif
1426 1.1 thorpej
1427 1.21.2.3 bouyer /*
1428 1.21.2.3 bouyer * Bulk protocol specific functions
1429 1.21.2.3 bouyer */
1430 1.21.2.3 bouyer
1431 1.21.2.3 bouyer Static void
1432 1.21.2.3 bouyer umass_bbb_reset(struct umass_softc *sc, int status)
1433 1.1 thorpej {
1434 1.1 thorpej usbd_device_handle dev;
1435 1.21.2.3 bouyer
1436 1.21.2.3 bouyer KASSERT(sc->proto & PROTO_BBB,
1437 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_bbb_reset\n", sc->proto));
1438 1.21.2.3 bouyer
1439 1.21.2.3 bouyer if (sc->sc_dying)
1440 1.21.2.3 bouyer return;
1441 1.1 thorpej
1442 1.1 thorpej /*
1443 1.1 thorpej * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1444 1.1 thorpej *
1445 1.1 thorpej * For Reset Recovery the host shall issue in the following order:
1446 1.1 thorpej * a) a Bulk-Only Mass Storage Reset
1447 1.1 thorpej * b) a Clear Feature HALT to the Bulk-In endpoint
1448 1.1 thorpej * c) a Clear Feature HALT to the Bulk-Out endpoint
1449 1.21.2.3 bouyer *
1450 1.21.2.3 bouyer * This is done in 3 steps, states:
1451 1.21.2.3 bouyer * TSTATE_BBB_RESET1
1452 1.21.2.3 bouyer * TSTATE_BBB_RESET2
1453 1.21.2.3 bouyer * TSTATE_BBB_RESET3
1454 1.21.2.3 bouyer *
1455 1.21.2.3 bouyer * If the reset doesn't succeed, the device should be port reset.
1456 1.1 thorpej */
1457 1.1 thorpej
1458 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1459 1.1 thorpej USBDEVNAME(sc->sc_dev)));
1460 1.21.2.3 bouyer
1461 1.21.2.3 bouyer sc->transfer_state = TSTATE_BBB_RESET1;
1462 1.21.2.3 bouyer sc->transfer_status = status;
1463 1.1 thorpej
1464 1.21.2.3 bouyer usbd_interface2device_handle(sc->iface, &dev);
1465 1.1 thorpej
1466 1.21.2.3 bouyer /* reset is a class specific interface write */
1467 1.21.2.3 bouyer sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1468 1.21.2.3 bouyer sc->request.bRequest = UR_BBB_RESET;
1469 1.21.2.3 bouyer USETW(sc->request.wValue, 0);
1470 1.21.2.3 bouyer USETW(sc->request.wIndex, sc->ifaceno);
1471 1.21.2.3 bouyer USETW(sc->request.wLength, 0);
1472 1.21.2.3 bouyer umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0,
1473 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_RESET1]);
1474 1.21.2.3 bouyer }
1475 1.1 thorpej
1476 1.21.2.3 bouyer Static void
1477 1.21.2.3 bouyer umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1478 1.21.2.3 bouyer void *data, int datalen, int dir,
1479 1.21.2.3 bouyer transfer_cb_f cb, void *priv)
1480 1.21.2.3 bouyer {
1481 1.21.2.3 bouyer static int dCBWtag = 42; /* unique for CBW of transfer */
1482 1.1 thorpej
1483 1.21.2.3 bouyer DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
1484 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), *(u_char*)cmd));
1485 1.21.2.3 bouyer
1486 1.21.2.3 bouyer KASSERT(sc->proto & PROTO_BBB,
1487 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_bbb_transfer\n",
1488 1.21.2.3 bouyer sc->proto));
1489 1.1 thorpej
1490 1.1 thorpej /*
1491 1.21.2.3 bouyer * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1492 1.21.2.3 bouyer * a data phase of datalen bytes from/to the device and finally a
1493 1.21.2.3 bouyer * csw read phase.
1494 1.21.2.3 bouyer * If the data direction was inbound a maximum of datalen bytes
1495 1.21.2.3 bouyer * is stored in the buffer pointed to by data.
1496 1.21.2.3 bouyer *
1497 1.21.2.3 bouyer * umass_bbb_transfer initialises the transfer and lets the state
1498 1.21.2.3 bouyer * machine in umass_bbb_state handle the completion. It uses the
1499 1.21.2.3 bouyer * following states:
1500 1.21.2.3 bouyer * TSTATE_BBB_COMMAND
1501 1.21.2.3 bouyer * -> TSTATE_BBB_DATA
1502 1.21.2.3 bouyer * -> TSTATE_BBB_STATUS
1503 1.21.2.3 bouyer * -> TSTATE_BBB_STATUS2
1504 1.21.2.3 bouyer * -> TSTATE_BBB_IDLE
1505 1.21.2.3 bouyer *
1506 1.21.2.3 bouyer * An error in any of those states will invoke
1507 1.21.2.3 bouyer * umass_bbb_reset.
1508 1.1 thorpej */
1509 1.1 thorpej
1510 1.1 thorpej /* check the given arguments */
1511 1.21.2.3 bouyer KASSERT(datalen == 0 || data != NULL,
1512 1.21.2.3 bouyer ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1513 1.21.2.3 bouyer KASSERT(cmdlen <= CBWCDBLENGTH,
1514 1.21.2.3 bouyer ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1515 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1516 1.21.2.3 bouyer KASSERT(dir == DIR_NONE || datalen > 0,
1517 1.21.2.3 bouyer ("%s: datalen == 0 while direction is not NONE\n",
1518 1.1 thorpej USBDEVNAME(sc->sc_dev)));
1519 1.21.2.3 bouyer KASSERT(datalen == 0 || dir != DIR_NONE,
1520 1.21.2.3 bouyer ("%s: direction is NONE while datalen is not zero\n",
1521 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1522 1.21.2.3 bouyer KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1523 1.21.2.3 bouyer ("%s: CBW struct does not have the right size (%d vs. %d)\n",
1524 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1525 1.21.2.3 bouyer sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1526 1.21.2.3 bouyer KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1527 1.21.2.3 bouyer ("%s: CSW struct does not have the right size (%d vs. %d)\n",
1528 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1529 1.21.2.3 bouyer sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1530 1.1 thorpej
1531 1.1 thorpej /*
1532 1.21.2.3 bouyer * Determine the direction of the data transfer and the length.
1533 1.1 thorpej *
1534 1.1 thorpej * dCBWDataTransferLength (datalen) :
1535 1.1 thorpej * This field indicates the number of bytes of data that the host
1536 1.1 thorpej * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1537 1.1 thorpej * the Direction bit) during the execution of this command. If this
1538 1.1 thorpej * field is set to 0, the device will expect that no data will be
1539 1.1 thorpej * transferred IN or OUT during this command, regardless of the value
1540 1.1 thorpej * of the Direction bit defined in dCBWFlags.
1541 1.1 thorpej *
1542 1.1 thorpej * dCBWFlags (dir) :
1543 1.1 thorpej * The bits of the Flags field are defined as follows:
1544 1.21.2.3 bouyer * Bits 0-6 reserved
1545 1.21.2.3 bouyer * Bit 7 Direction - this bit shall be ignored if the
1546 1.21.2.3 bouyer * dCBWDataTransferLength field is zero.
1547 1.21.2.3 bouyer * 0 = data Out from host to device
1548 1.21.2.3 bouyer * 1 = data In from device to host
1549 1.1 thorpej */
1550 1.1 thorpej
1551 1.1 thorpej /* Fill in the Command Block Wrapper */
1552 1.21.2.3 bouyer USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1553 1.21.2.3 bouyer USETDW(sc->cbw.dCBWTag, dCBWtag);
1554 1.21.2.3 bouyer dCBWtag++; /* cannot be done in macro (it will be done 4 times) */
1555 1.21.2.3 bouyer USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1556 1.21.2.3 bouyer /* DIR_NONE is treated as DIR_OUT (0x00) */
1557 1.21.2.3 bouyer sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1558 1.21.2.3 bouyer sc->cbw.bCBWLUN = lun;
1559 1.21.2.3 bouyer sc->cbw.bCDBLength = cmdlen;
1560 1.21.2.3 bouyer bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1561 1.21.2.3 bouyer
1562 1.21.2.3 bouyer DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1563 1.21.2.3 bouyer
1564 1.21.2.3 bouyer /* store the details for the data transfer phase */
1565 1.21.2.3 bouyer sc->transfer_dir = dir;
1566 1.21.2.3 bouyer sc->transfer_data = data;
1567 1.21.2.3 bouyer sc->transfer_datalen = datalen;
1568 1.21.2.3 bouyer sc->transfer_actlen = 0;
1569 1.21.2.3 bouyer sc->transfer_cb = cb;
1570 1.21.2.3 bouyer sc->transfer_priv = priv;
1571 1.21.2.3 bouyer sc->transfer_status = STATUS_CMD_OK;
1572 1.21.2.3 bouyer
1573 1.21.2.3 bouyer /* move from idle to the command state */
1574 1.21.2.3 bouyer sc->transfer_state = TSTATE_BBB_COMMAND;
1575 1.1 thorpej
1576 1.1 thorpej /* Send the CBW from host to device via bulk-out endpoint. */
1577 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkout_pipe,
1578 1.21.2.3 bouyer &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1579 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_CBW])) {
1580 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1581 1.1 thorpej }
1582 1.21.2.3 bouyer }
1583 1.1 thorpej
1584 1.1 thorpej
1585 1.21.2.3 bouyer Static void
1586 1.21.2.3 bouyer umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1587 1.21.2.3 bouyer usbd_status err)
1588 1.21.2.3 bouyer {
1589 1.21.2.3 bouyer struct umass_softc *sc = (struct umass_softc *) priv;
1590 1.21.2.3 bouyer usbd_xfer_handle next_xfer;
1591 1.1 thorpej
1592 1.21.2.3 bouyer KASSERT(sc->proto & PROTO_BBB,
1593 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto));
1594 1.1 thorpej
1595 1.21.2.3 bouyer if (sc->sc_dying)
1596 1.21.2.3 bouyer return;
1597 1.1 thorpej
1598 1.1 thorpej /*
1599 1.21.2.3 bouyer * State handling for BBB transfers.
1600 1.21.2.3 bouyer *
1601 1.21.2.3 bouyer * The subroutine is rather long. It steps through the states given in
1602 1.21.2.3 bouyer * Annex A of the Bulk-Only specification.
1603 1.21.2.3 bouyer * Each state first does the error handling of the previous transfer
1604 1.21.2.3 bouyer * and then prepares the next transfer.
1605 1.21.2.3 bouyer * Each transfer is done asynchroneously so after the request/transfer
1606 1.21.2.3 bouyer * has been submitted you will find a 'return;'.
1607 1.1 thorpej */
1608 1.1 thorpej
1609 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1610 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->transfer_state,
1611 1.21.2.3 bouyer states[sc->transfer_state], xfer, usbd_errstr(err)));
1612 1.21.2.3 bouyer
1613 1.21.2.3 bouyer switch (sc->transfer_state) {
1614 1.21.2.3 bouyer
1615 1.21.2.3 bouyer /***** Bulk Transfer *****/
1616 1.21.2.3 bouyer case TSTATE_BBB_COMMAND:
1617 1.21.2.3 bouyer /* Command transport phase, error handling */
1618 1.21.2.3 bouyer if (err) {
1619 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1620 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1621 1.21.2.3 bouyer /* If the device detects that the CBW is invalid, then
1622 1.21.2.3 bouyer * the device may STALL both bulk endpoints and require
1623 1.21.2.3 bouyer * a Bulk-Reset
1624 1.21.2.3 bouyer */
1625 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1626 1.21.2.3 bouyer return;
1627 1.1 thorpej }
1628 1.1 thorpej
1629 1.21.2.3 bouyer /* Data transport phase, setup transfer */
1630 1.21.2.3 bouyer sc->transfer_state = TSTATE_BBB_DATA;
1631 1.21.2.3 bouyer if (sc->transfer_dir == DIR_IN) {
1632 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkin_pipe,
1633 1.21.2.3 bouyer sc->data_buffer, sc->transfer_datalen,
1634 1.21.2.3 bouyer USBD_SHORT_XFER_OK | USBD_NO_COPY,
1635 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_DATA]))
1636 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1637 1.1 thorpej
1638 1.21.2.3 bouyer return;
1639 1.21.2.3 bouyer } else if (sc->transfer_dir == DIR_OUT) {
1640 1.21.2.3 bouyer memcpy(sc->data_buffer, sc->transfer_data,
1641 1.21.2.3 bouyer sc->transfer_datalen);
1642 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkout_pipe,
1643 1.21.2.3 bouyer sc->data_buffer, sc->transfer_datalen,
1644 1.21.2.3 bouyer USBD_NO_COPY,/* fixed length transfer */
1645 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_DATA]))
1646 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1647 1.21.2.3 bouyer
1648 1.21.2.3 bouyer return;
1649 1.21.2.3 bouyer } else {
1650 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1651 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1652 1.21.2.3 bouyer }
1653 1.21.2.3 bouyer
1654 1.21.2.3 bouyer /* FALLTHROUGH if no data phase, err == 0 */
1655 1.21.2.3 bouyer case TSTATE_BBB_DATA:
1656 1.21.2.3 bouyer /* Command transport phase, error handling (ignored if no data
1657 1.21.2.3 bouyer * phase (fallthrough from previous state)) */
1658 1.21.2.3 bouyer if (sc->transfer_dir != DIR_NONE) {
1659 1.21.2.3 bouyer /* retrieve the length of the transfer that was done */
1660 1.21.2.3 bouyer usbd_get_xfer_status(xfer, NULL, NULL,
1661 1.21.2.3 bouyer &sc->transfer_actlen, NULL);
1662 1.21.2.3 bouyer
1663 1.21.2.3 bouyer if (err) {
1664 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
1665 1.21.2.3 bouyer "%s\n", USBDEVNAME(sc->sc_dev),
1666 1.21.2.3 bouyer (sc->transfer_dir == DIR_IN?"in":"out"),
1667 1.21.2.3 bouyer sc->transfer_datalen,usbd_errstr(err)));
1668 1.21.2.3 bouyer
1669 1.21.2.3 bouyer if (err == USBD_STALLED) {
1670 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
1671 1.21.2.3 bouyer (sc->transfer_dir == DIR_IN?
1672 1.21.2.3 bouyer sc->bulkin:sc->bulkout),
1673 1.21.2.3 bouyer (sc->transfer_dir == DIR_IN?
1674 1.21.2.3 bouyer sc->bulkin_pipe:sc->bulkout_pipe),
1675 1.21.2.3 bouyer TSTATE_BBB_DCLEAR,
1676 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_DCLEAR]);
1677 1.21.2.3 bouyer return;
1678 1.21.2.3 bouyer } else {
1679 1.21.2.3 bouyer /* Unless the error is a pipe stall the
1680 1.21.2.3 bouyer * error is fatal.
1681 1.21.2.3 bouyer */
1682 1.21.2.3 bouyer umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1683 1.21.2.3 bouyer return;
1684 1.21.2.3 bouyer }
1685 1.21.2.3 bouyer }
1686 1.21.2.3 bouyer }
1687 1.21.2.3 bouyer
1688 1.21.2.3 bouyer if (sc->transfer_dir == DIR_IN)
1689 1.21.2.3 bouyer memcpy(sc->transfer_data, sc->data_buffer,
1690 1.21.2.3 bouyer sc->transfer_actlen);
1691 1.21.2.3 bouyer
1692 1.21.2.3 bouyer DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1693 1.21.2.3 bouyer umass_dump_buffer(sc, sc->transfer_data,
1694 1.21.2.3 bouyer sc->transfer_datalen, 48));
1695 1.21.2.3 bouyer
1696 1.21.2.3 bouyer /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1697 1.21.2.3 bouyer case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1698 1.21.2.3 bouyer case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1699 1.21.2.3 bouyer /* Reading of CSW after bulk stall condition in data phase
1700 1.21.2.3 bouyer * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1701 1.21.2.3 bouyer * reading CSW (TSTATE_BBB_SCLEAR).
1702 1.21.2.3 bouyer * In the case of no data phase or successfull data phase,
1703 1.21.2.3 bouyer * err == 0 and the following if block is passed.
1704 1.21.2.3 bouyer */
1705 1.21.2.3 bouyer if (err) { /* should not occur */
1706 1.21.2.3 bouyer /* try the transfer below, even if clear stall failed */
1707 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1708 1.21.2.3 bouyer ", %s\n", USBDEVNAME(sc->sc_dev),
1709 1.21.2.3 bouyer (sc->transfer_dir == DIR_IN? "in":"out"),
1710 1.21.2.3 bouyer usbd_errstr(err)));
1711 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1712 1.21.2.3 bouyer return;
1713 1.21.2.3 bouyer }
1714 1.21.2.3 bouyer
1715 1.21.2.3 bouyer /* Status transport phase, setup transfer */
1716 1.21.2.3 bouyer if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1717 1.21.2.3 bouyer sc->transfer_state == TSTATE_BBB_DATA ||
1718 1.21.2.3 bouyer sc->transfer_state == TSTATE_BBB_DCLEAR) {
1719 1.21.2.3 bouyer /* After no data phase, successfull data phase and
1720 1.21.2.3 bouyer * after clearing bulk-in/-out stall condition
1721 1.21.2.3 bouyer */
1722 1.21.2.3 bouyer sc->transfer_state = TSTATE_BBB_STATUS1;
1723 1.21.2.3 bouyer next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1724 1.21.2.3 bouyer } else {
1725 1.21.2.3 bouyer /* After first attempt of fetching CSW */
1726 1.21.2.3 bouyer sc->transfer_state = TSTATE_BBB_STATUS2;
1727 1.21.2.3 bouyer next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1728 1.21.2.3 bouyer }
1729 1.21.2.3 bouyer
1730 1.21.2.3 bouyer /* Read the Command Status Wrapper via bulk-in endpoint. */
1731 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkin_pipe,
1732 1.21.2.3 bouyer &sc->csw, UMASS_BBB_CSW_SIZE, 0,
1733 1.21.2.3 bouyer next_xfer)) {
1734 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1735 1.21.2.3 bouyer return;
1736 1.21.2.3 bouyer }
1737 1.21.2.3 bouyer
1738 1.21.2.3 bouyer return;
1739 1.21.2.3 bouyer case TSTATE_BBB_STATUS1: /* first attempt */
1740 1.21.2.3 bouyer case TSTATE_BBB_STATUS2: /* second attempt */
1741 1.21.2.3 bouyer /* Status transfer, error handling */
1742 1.1 thorpej if (err) {
1743 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1744 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1745 1.21.2.3 bouyer (sc->transfer_state == TSTATE_BBB_STATUS1?
1746 1.21.2.3 bouyer ", retrying":"")));
1747 1.21.2.3 bouyer
1748 1.21.2.3 bouyer /* If this was the first attempt at fetching the CSW
1749 1.21.2.3 bouyer * retry it, otherwise fail.
1750 1.21.2.3 bouyer */
1751 1.21.2.3 bouyer if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1752 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
1753 1.21.2.3 bouyer sc->bulkin, sc->bulkin_pipe,
1754 1.21.2.3 bouyer TSTATE_BBB_SCLEAR,
1755 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_SCLEAR]);
1756 1.21.2.3 bouyer return;
1757 1.21.2.3 bouyer } else {
1758 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1759 1.21.2.3 bouyer return;
1760 1.21.2.3 bouyer }
1761 1.21.2.3 bouyer }
1762 1.21.2.3 bouyer
1763 1.21.2.3 bouyer DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1764 1.21.2.3 bouyer
1765 1.21.2.3 bouyer /* Check CSW and handle any error */
1766 1.21.2.3 bouyer if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1767 1.21.2.3 bouyer /* Invalid CSW: Wrong signature or wrong tag might
1768 1.21.2.3 bouyer * indicate that the device is confused -> reset it.
1769 1.21.2.3 bouyer */
1770 1.21.2.3 bouyer printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1771 1.1 thorpej USBDEVNAME(sc->sc_dev),
1772 1.21.2.3 bouyer UGETDW(sc->csw.dCSWSignature),
1773 1.21.2.3 bouyer CSWSIGNATURE);
1774 1.21.2.3 bouyer
1775 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1776 1.21.2.3 bouyer return;
1777 1.21.2.3 bouyer } else if (UGETDW(sc->csw.dCSWTag)
1778 1.21.2.3 bouyer != UGETDW(sc->cbw.dCBWTag)) {
1779 1.21.2.3 bouyer printf("%s: Invalid CSW: tag %d should be %d\n",
1780 1.1 thorpej USBDEVNAME(sc->sc_dev),
1781 1.21.2.3 bouyer UGETDW(sc->csw.dCSWTag),
1782 1.21.2.3 bouyer UGETDW(sc->cbw.dCBWTag));
1783 1.1 thorpej
1784 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1785 1.21.2.3 bouyer return;
1786 1.1 thorpej
1787 1.21.2.3 bouyer /* CSW is valid here */
1788 1.21.2.3 bouyer } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1789 1.21.2.3 bouyer printf("%s: Invalid CSW: status %d > %d\n",
1790 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1791 1.21.2.3 bouyer sc->csw.bCSWStatus,
1792 1.21.2.3 bouyer CSWSTATUS_PHASE);
1793 1.1 thorpej
1794 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1795 1.21.2.3 bouyer return;
1796 1.21.2.3 bouyer } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1797 1.21.2.3 bouyer printf("%s: Phase Error, residue = %d\n",
1798 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1799 1.21.2.3 bouyer UGETDW(sc->csw.dCSWDataResidue));
1800 1.21.2.3 bouyer
1801 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1802 1.21.2.3 bouyer return;
1803 1.21.2.3 bouyer
1804 1.21.2.3 bouyer } else if (sc->transfer_actlen > sc->transfer_datalen) {
1805 1.21.2.3 bouyer /* Buffer overrun! Don't let this go by unnoticed */
1806 1.21.2.3 bouyer panic("%s: transferred %d bytes instead of %d bytes\n",
1807 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1808 1.21.2.3 bouyer sc->transfer_actlen, sc->transfer_datalen);
1809 1.21.2.3 bouyer } else if (sc->transfer_datalen - sc->transfer_actlen
1810 1.21.2.3 bouyer != UGETDW(sc->csw.dCSWDataResidue)) {
1811 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1812 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1813 1.21.2.3 bouyer sc->transfer_datalen - sc->transfer_actlen,
1814 1.21.2.3 bouyer UGETDW(sc->csw.dCSWDataResidue)));
1815 1.1 thorpej
1816 1.21.2.3 bouyer umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1817 1.21.2.3 bouyer return;
1818 1.1 thorpej
1819 1.21.2.3 bouyer } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1820 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1821 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1822 1.21.2.3 bouyer UGETDW(sc->csw.dCSWDataResidue)));
1823 1.1 thorpej
1824 1.21.2.3 bouyer /* SCSI command failed but transfer was succesful */
1825 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
1826 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
1827 1.21.2.3 bouyer UGETDW(sc->csw.dCSWDataResidue),
1828 1.21.2.3 bouyer STATUS_CMD_FAILED);
1829 1.1 thorpej
1830 1.21.2.3 bouyer return;
1831 1.15 thorpej
1832 1.21.2.3 bouyer } else { /* success */
1833 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
1834 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
1835 1.21.2.3 bouyer UGETDW(sc->csw.dCSWDataResidue),
1836 1.21.2.3 bouyer STATUS_CMD_OK);
1837 1.1 thorpej
1838 1.21.2.1 thorpej return;
1839 1.1 thorpej }
1840 1.1 thorpej
1841 1.21.2.3 bouyer /***** Bulk Reset *****/
1842 1.21.2.3 bouyer case TSTATE_BBB_RESET1:
1843 1.21.2.3 bouyer if (err)
1844 1.21.2.3 bouyer printf("%s: BBB reset failed, %s\n",
1845 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1846 1.21.2.3 bouyer
1847 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
1848 1.21.2.3 bouyer sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
1849 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_RESET2]);
1850 1.21.2.3 bouyer
1851 1.21.2.3 bouyer return;
1852 1.21.2.3 bouyer case TSTATE_BBB_RESET2:
1853 1.21.2.3 bouyer if (err) /* should not occur */
1854 1.21.2.3 bouyer printf("%s: BBB bulk-in clear stall failed, %s\n",
1855 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1856 1.21.2.3 bouyer /* no error recovery, otherwise we end up in a loop */
1857 1.21.2.3 bouyer
1858 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
1859 1.21.2.3 bouyer sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
1860 1.21.2.3 bouyer sc->transfer_xfer[XFER_BBB_RESET3]);
1861 1.21.2.3 bouyer
1862 1.21.2.3 bouyer return;
1863 1.21.2.3 bouyer case TSTATE_BBB_RESET3:
1864 1.21.2.3 bouyer if (err) /* should not occur */
1865 1.21.2.3 bouyer printf("%s: BBB bulk-out clear stall failed, %s\n",
1866 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1867 1.21.2.3 bouyer /* no error recovery, otherwise we end up in a loop */
1868 1.21.2.3 bouyer
1869 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
1870 1.21.2.3 bouyer if (sc->transfer_priv) {
1871 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
1872 1.21.2.3 bouyer sc->transfer_datalen,
1873 1.21.2.3 bouyer sc->transfer_status);
1874 1.21.2.3 bouyer }
1875 1.21.2.3 bouyer
1876 1.21.2.3 bouyer return;
1877 1.21.2.3 bouyer
1878 1.21.2.3 bouyer /***** Default *****/
1879 1.21.2.3 bouyer default:
1880 1.21.2.3 bouyer panic("%s: Unknown state %d\n",
1881 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->transfer_state);
1882 1.21.2.3 bouyer }
1883 1.21.2.3 bouyer }
1884 1.21.2.3 bouyer
1885 1.21.2.3 bouyer /*
1886 1.21.2.3 bouyer * Command/Bulk/Interrupt (CBI) specific functions
1887 1.21.2.3 bouyer */
1888 1.21.2.3 bouyer
1889 1.21.2.3 bouyer Static int
1890 1.21.2.3 bouyer umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1891 1.21.2.3 bouyer usbd_xfer_handle xfer)
1892 1.21.2.3 bouyer {
1893 1.21.2.3 bouyer usbd_device_handle dev;
1894 1.21.2.3 bouyer
1895 1.21.2.3 bouyer KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1896 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto));
1897 1.21.2.3 bouyer
1898 1.21.2.3 bouyer usbd_interface2device_handle(sc->iface, &dev);
1899 1.21.2.3 bouyer
1900 1.21.2.3 bouyer sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1901 1.21.2.3 bouyer sc->request.bRequest = UR_CBI_ADSC;
1902 1.21.2.3 bouyer USETW(sc->request.wValue, 0);
1903 1.21.2.3 bouyer USETW(sc->request.wIndex, sc->ifaceno);
1904 1.21.2.3 bouyer USETW(sc->request.wLength, buflen);
1905 1.21.2.3 bouyer return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer,
1906 1.21.2.3 bouyer buflen, 0, xfer);
1907 1.21.2.3 bouyer }
1908 1.21.2.3 bouyer
1909 1.21.2.3 bouyer
1910 1.21.2.3 bouyer Static void
1911 1.21.2.3 bouyer umass_cbi_reset(struct umass_softc *sc, int status)
1912 1.21.2.3 bouyer {
1913 1.21.2.3 bouyer int i;
1914 1.21.2.3 bouyer # define SEND_DIAGNOSTIC_CMDLEN 12
1915 1.21.2.3 bouyer
1916 1.21.2.3 bouyer KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1917 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto));
1918 1.21.2.3 bouyer
1919 1.21.2.3 bouyer if (sc->sc_dying)
1920 1.21.2.3 bouyer return;
1921 1.21.2.3 bouyer
1922 1.21.2.3 bouyer /*
1923 1.21.2.3 bouyer * Command Block Reset Protocol
1924 1.21.2.3 bouyer *
1925 1.21.2.3 bouyer * First send a reset request to the device. Then clear
1926 1.21.2.3 bouyer * any possibly stalled bulk endpoints.
1927 1.21.2.3 bouyer
1928 1.21.2.3 bouyer * This is done in 3 steps, states:
1929 1.21.2.3 bouyer * TSTATE_CBI_RESET1
1930 1.21.2.3 bouyer * TSTATE_CBI_RESET2
1931 1.21.2.3 bouyer * TSTATE_CBI_RESET3
1932 1.21.2.3 bouyer *
1933 1.21.2.3 bouyer * If the reset doesn't succeed, the device should be port reset.
1934 1.21.2.3 bouyer */
1935 1.21.2.3 bouyer
1936 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1937 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1938 1.21.2.3 bouyer
1939 1.21.2.3 bouyer KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1940 1.21.2.3 bouyer ("%s: CBL struct is too small (%d < %d)\n",
1941 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
1942 1.21.2.3 bouyer sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1943 1.21.2.3 bouyer
1944 1.21.2.3 bouyer sc->transfer_state = TSTATE_CBI_RESET1;
1945 1.21.2.3 bouyer sc->transfer_status = status;
1946 1.21.2.3 bouyer
1947 1.21.2.3 bouyer /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1948 1.21.2.3 bouyer * the two the last 10 bytes of the cbl is filled with 0xff (section
1949 1.21.2.3 bouyer * 2.2 of the CBI spec).
1950 1.21.2.3 bouyer */
1951 1.21.2.3 bouyer sc->cbl[0] = 0x1d; /* Command Block Reset */
1952 1.21.2.3 bouyer sc->cbl[1] = 0x04;
1953 1.21.2.3 bouyer for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1954 1.21.2.3 bouyer sc->cbl[i] = 0xff;
1955 1.21.2.3 bouyer
1956 1.21.2.3 bouyer umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1957 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_RESET1]);
1958 1.21.2.3 bouyer /* XXX if the command fails we should reset the port on the bub */
1959 1.21.2.3 bouyer }
1960 1.21.2.3 bouyer
1961 1.21.2.3 bouyer Static void
1962 1.21.2.3 bouyer umass_cbi_transfer(struct umass_softc *sc, int lun,
1963 1.21.2.3 bouyer void *cmd, int cmdlen, void *data, int datalen, int dir,
1964 1.21.2.3 bouyer transfer_cb_f cb, void *priv)
1965 1.21.2.3 bouyer {
1966 1.21.2.3 bouyer DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1967 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen));
1968 1.21.2.3 bouyer
1969 1.21.2.3 bouyer KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1970 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_cbi_transfer\n",
1971 1.21.2.3 bouyer sc->proto));
1972 1.21.2.3 bouyer
1973 1.21.2.3 bouyer if (sc->sc_dying)
1974 1.21.2.3 bouyer return;
1975 1.21.2.3 bouyer
1976 1.21.2.3 bouyer /*
1977 1.21.2.3 bouyer * Do a CBI transfer with cmdlen bytes from cmd, possibly
1978 1.21.2.3 bouyer * a data phase of datalen bytes from/to the device and finally a
1979 1.21.2.3 bouyer * csw read phase.
1980 1.21.2.3 bouyer * If the data direction was inbound a maximum of datalen bytes
1981 1.21.2.3 bouyer * is stored in the buffer pointed to by data.
1982 1.21.2.3 bouyer *
1983 1.21.2.3 bouyer * umass_cbi_transfer initialises the transfer and lets the state
1984 1.21.2.3 bouyer * machine in umass_cbi_state handle the completion. It uses the
1985 1.21.2.3 bouyer * following states:
1986 1.21.2.3 bouyer * TSTATE_CBI_COMMAND
1987 1.21.2.3 bouyer * -> XXX fill in
1988 1.21.2.3 bouyer *
1989 1.21.2.3 bouyer * An error in any of those states will invoke
1990 1.21.2.3 bouyer * umass_cbi_reset.
1991 1.21.2.3 bouyer */
1992 1.21.2.3 bouyer
1993 1.21.2.3 bouyer /* check the given arguments */
1994 1.21.2.3 bouyer KASSERT(datalen == 0 || data != NULL,
1995 1.21.2.3 bouyer ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1996 1.21.2.3 bouyer KASSERT(datalen == 0 || dir != DIR_NONE,
1997 1.21.2.3 bouyer ("%s: direction is NONE while datalen is not zero\n",
1998 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
1999 1.21.2.3 bouyer
2000 1.21.2.3 bouyer /* store the details for the data transfer phase */
2001 1.21.2.3 bouyer sc->transfer_dir = dir;
2002 1.21.2.3 bouyer sc->transfer_data = data;
2003 1.21.2.3 bouyer sc->transfer_datalen = datalen;
2004 1.21.2.3 bouyer sc->transfer_actlen = 0;
2005 1.21.2.3 bouyer sc->transfer_cb = cb;
2006 1.21.2.3 bouyer sc->transfer_priv = priv;
2007 1.21.2.3 bouyer sc->transfer_status = STATUS_CMD_OK;
2008 1.21.2.3 bouyer
2009 1.21.2.3 bouyer /* move from idle to the command state */
2010 1.21.2.3 bouyer sc->transfer_state = TSTATE_CBI_COMMAND;
2011 1.21.2.3 bouyer
2012 1.21.2.3 bouyer /* Send the Command Block from host to device via control endpoint. */
2013 1.21.2.3 bouyer if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2014 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2015 1.21.2.3 bouyer }
2016 1.21.2.3 bouyer
2017 1.21.2.3 bouyer Static void
2018 1.21.2.3 bouyer umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2019 1.21.2.3 bouyer usbd_status err)
2020 1.21.2.3 bouyer {
2021 1.21.2.3 bouyer struct umass_softc *sc = (struct umass_softc *) priv;
2022 1.21.2.3 bouyer
2023 1.21.2.3 bouyer KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
2024 1.21.2.3 bouyer ("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto));
2025 1.21.2.3 bouyer
2026 1.21.2.3 bouyer if (sc->sc_dying)
2027 1.21.2.3 bouyer return;
2028 1.21.2.3 bouyer
2029 1.21.2.3 bouyer /*
2030 1.21.2.3 bouyer * State handling for CBI transfers.
2031 1.21.2.3 bouyer */
2032 1.21.2.3 bouyer
2033 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2034 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->transfer_state,
2035 1.21.2.3 bouyer states[sc->transfer_state], xfer, usbd_errstr(err)));
2036 1.21.2.3 bouyer
2037 1.21.2.3 bouyer switch (sc->transfer_state) {
2038 1.21.2.3 bouyer
2039 1.21.2.3 bouyer /***** CBI Transfer *****/
2040 1.21.2.3 bouyer case TSTATE_CBI_COMMAND:
2041 1.21.2.3 bouyer if (err == USBD_STALLED) {
2042 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2043 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2044 1.21.2.3 bouyer /* Status transport by control pipe (section 2.3.2.1).
2045 1.21.2.3 bouyer * The command contained in the command block failed.
2046 1.21.2.3 bouyer *
2047 1.21.2.3 bouyer * The control pipe has already been unstalled by the
2048 1.21.2.3 bouyer * USB stack.
2049 1.21.2.3 bouyer * Section 2.4.3.1.1 states that the bulk in endpoints
2050 1.21.2.3 bouyer * should not stalled at this point.
2051 1.21.2.3 bouyer */
2052 1.21.2.3 bouyer
2053 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2054 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2055 1.21.2.3 bouyer sc->transfer_datalen,
2056 1.21.2.3 bouyer STATUS_CMD_FAILED);
2057 1.21.2.3 bouyer
2058 1.21.2.3 bouyer return;
2059 1.21.2.3 bouyer } else if (err) {
2060 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2061 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2062 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2063 1.21.2.3 bouyer
2064 1.21.2.3 bouyer return;
2065 1.21.2.3 bouyer }
2066 1.21.2.3 bouyer
2067 1.21.2.3 bouyer sc->transfer_state = TSTATE_CBI_DATA;
2068 1.21.2.3 bouyer if (sc->transfer_dir == DIR_IN) {
2069 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkin_pipe,
2070 1.21.2.3 bouyer sc->transfer_data, sc->transfer_datalen,
2071 1.21.2.3 bouyer USBD_SHORT_XFER_OK | USBD_NO_COPY,
2072 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_DATA]))
2073 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2074 1.21.2.3 bouyer
2075 1.21.2.3 bouyer } else if (sc->transfer_dir == DIR_OUT) {
2076 1.21.2.3 bouyer memcpy(sc->data_buffer, sc->transfer_data,
2077 1.21.2.3 bouyer sc->transfer_datalen);
2078 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->bulkout_pipe,
2079 1.21.2.3 bouyer sc->transfer_data, sc->transfer_datalen,
2080 1.21.2.3 bouyer USBD_NO_COPY,/* fixed length transfer */
2081 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_DATA]))
2082 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2083 1.21.2.3 bouyer
2084 1.21.2.3 bouyer } else if (sc->proto & PROTO_CBI_I) {
2085 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2086 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2087 1.21.2.3 bouyer sc->transfer_state = TSTATE_CBI_STATUS;
2088 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->intrin_pipe,
2089 1.21.2.3 bouyer &sc->sbl, sizeof(sc->sbl),
2090 1.21.2.3 bouyer 0, /* fixed length transfer */
2091 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_STATUS])){
2092 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2093 1.21.2.3 bouyer }
2094 1.21.2.3 bouyer } else {
2095 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2096 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2097 1.21.2.3 bouyer /* No command completion interrupt. Request
2098 1.21.2.3 bouyer * sense data.
2099 1.21.2.3 bouyer */
2100 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2101 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2102 1.21.2.3 bouyer 0, STATUS_CMD_UNKNOWN);
2103 1.21.2.3 bouyer }
2104 1.21.2.3 bouyer
2105 1.21.2.3 bouyer return;
2106 1.21.2.3 bouyer
2107 1.21.2.3 bouyer case TSTATE_CBI_DATA:
2108 1.21.2.3 bouyer /* retrieve the length of the transfer that was done */
2109 1.21.2.3 bouyer usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2110 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
2111 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
2112 1.21.2.3 bouyer
2113 1.21.2.3 bouyer if (err) {
2114 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2115 1.21.2.3 bouyer "%s\n", USBDEVNAME(sc->sc_dev),
2116 1.21.2.3 bouyer (sc->transfer_dir == DIR_IN?"in":"out"),
2117 1.21.2.3 bouyer sc->transfer_datalen,usbd_errstr(err)));
2118 1.21.2.3 bouyer
2119 1.21.2.3 bouyer if (err == USBD_STALLED) {
2120 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
2121 1.21.2.3 bouyer sc->bulkin, sc->bulkin_pipe,
2122 1.21.2.3 bouyer TSTATE_CBI_DCLEAR,
2123 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_DCLEAR]);
2124 1.21.2.3 bouyer } else {
2125 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2126 1.21.2.3 bouyer }
2127 1.21.2.3 bouyer return;
2128 1.21.2.3 bouyer }
2129 1.21.2.3 bouyer
2130 1.21.2.3 bouyer if (sc->transfer_dir == DIR_IN)
2131 1.21.2.3 bouyer memcpy(sc->transfer_data, sc->data_buffer,
2132 1.21.2.3 bouyer sc->transfer_actlen);
2133 1.21.2.3 bouyer
2134 1.21.2.3 bouyer DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2135 1.21.2.3 bouyer umass_dump_buffer(sc, sc->transfer_data,
2136 1.21.2.3 bouyer sc->transfer_actlen, 48));
2137 1.21.2.3 bouyer
2138 1.21.2.3 bouyer if (sc->proto & PROTO_CBI_I) {
2139 1.21.2.3 bouyer sc->transfer_state = TSTATE_CBI_STATUS;
2140 1.21.2.3 bouyer memset(&sc->sbl, 0, sizeof(sc->sbl));
2141 1.21.2.3 bouyer if (umass_setup_transfer(sc, sc->intrin_pipe,
2142 1.21.2.3 bouyer &sc->sbl, sizeof(sc->sbl),
2143 1.21.2.3 bouyer 0, /* fixed length transfer */
2144 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_STATUS])){
2145 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2146 1.21.2.3 bouyer }
2147 1.21.2.3 bouyer } else {
2148 1.21.2.3 bouyer /* No command completion interrupt. Request
2149 1.21.2.3 bouyer * sense to get status of command.
2150 1.21.2.3 bouyer */
2151 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2152 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2153 1.21.2.3 bouyer sc->transfer_datalen - sc->transfer_actlen,
2154 1.21.2.3 bouyer STATUS_CMD_UNKNOWN);
2155 1.21.2.3 bouyer }
2156 1.21.2.3 bouyer return;
2157 1.21.2.3 bouyer
2158 1.21.2.3 bouyer case TSTATE_CBI_STATUS:
2159 1.21.2.3 bouyer if (err) {
2160 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2161 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2162 1.21.2.3 bouyer /* Status transport by interrupt pipe (section 2.3.2.2).
2163 1.21.2.3 bouyer */
2164 1.21.2.3 bouyer
2165 1.21.2.3 bouyer if (err == USBD_STALLED) {
2166 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
2167 1.21.2.3 bouyer sc->intrin, sc->intrin_pipe,
2168 1.21.2.3 bouyer TSTATE_CBI_SCLEAR,
2169 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_SCLEAR]);
2170 1.21.2.3 bouyer } else {
2171 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2172 1.21.2.3 bouyer }
2173 1.21.2.3 bouyer return;
2174 1.21.2.3 bouyer }
2175 1.21.2.3 bouyer
2176 1.21.2.3 bouyer /* Dissect the information in the buffer */
2177 1.21.2.3 bouyer
2178 1.21.2.3 bouyer if (sc->proto & PROTO_UFI) {
2179 1.21.2.3 bouyer int status;
2180 1.21.2.3 bouyer
2181 1.21.2.3 bouyer /* Section 3.4.3.1.3 specifies that the UFI command
2182 1.21.2.3 bouyer * protocol returns an ASC and ASCQ in the interrupt
2183 1.21.2.3 bouyer * data block.
2184 1.21.2.3 bouyer */
2185 1.21.2.3 bouyer
2186 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2187 1.21.2.3 bouyer "ASCQ = 0x%02x\n",
2188 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
2189 1.21.2.3 bouyer sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2190 1.21.2.3 bouyer
2191 1.21.2.3 bouyer if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2192 1.21.2.3 bouyer status = STATUS_CMD_OK;
2193 1.21.2.3 bouyer else
2194 1.21.2.3 bouyer status = STATUS_CMD_FAILED;
2195 1.21.2.3 bouyer
2196 1.21.2.3 bouyer /* No sense, command successfull */
2197 1.21.2.3 bouyer } else {
2198 1.21.2.3 bouyer /* Command Interrupt Data Block */
2199 1.21.2.3 bouyer DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2200 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
2201 1.21.2.3 bouyer sc->sbl.common.type, sc->sbl.common.value));
2202 1.21.2.3 bouyer
2203 1.21.2.3 bouyer if (sc->sbl.common.type == IDB_TYPE_CCI) {
2204 1.21.2.3 bouyer int err;
2205 1.21.2.3 bouyer
2206 1.21.2.3 bouyer if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2207 1.21.2.3 bouyer == IDB_VALUE_PASS) {
2208 1.21.2.3 bouyer err = STATUS_CMD_OK;
2209 1.21.2.3 bouyer } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2210 1.21.2.3 bouyer == IDB_VALUE_FAIL ||
2211 1.21.2.3 bouyer (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2212 1.21.2.3 bouyer == IDB_VALUE_PERSISTENT) {
2213 1.21.2.3 bouyer err = STATUS_CMD_FAILED;
2214 1.21.2.3 bouyer } else {
2215 1.21.2.3 bouyer err = STATUS_WIRE_FAILED;
2216 1.21.2.3 bouyer }
2217 1.21.2.3 bouyer
2218 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2219 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2220 1.21.2.3 bouyer sc->transfer_datalen,
2221 1.21.2.3 bouyer err);
2222 1.21.2.3 bouyer }
2223 1.21.2.3 bouyer }
2224 1.21.2.3 bouyer return;
2225 1.21.2.3 bouyer
2226 1.21.2.3 bouyer case TSTATE_CBI_DCLEAR:
2227 1.21.2.3 bouyer if (err) { /* should not occur */
2228 1.21.2.3 bouyer printf("%s: CBI bulk-in/out stall clear failed, %s\n",
2229 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2230 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2231 1.21.2.3 bouyer }
2232 1.21.2.3 bouyer
2233 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2234 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2235 1.21.2.3 bouyer sc->transfer_datalen,
2236 1.21.2.3 bouyer STATUS_CMD_FAILED);
2237 1.21.2.3 bouyer return;
2238 1.21.2.3 bouyer
2239 1.21.2.3 bouyer case TSTATE_CBI_SCLEAR:
2240 1.21.2.3 bouyer if (err) /* should not occur */
2241 1.21.2.3 bouyer printf("%s: CBI intr-in stall clear failed, %s\n",
2242 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2243 1.21.2.3 bouyer
2244 1.21.2.3 bouyer /* Something really bad is going on. Reset the device */
2245 1.21.2.3 bouyer umass_cbi_reset(sc, STATUS_CMD_FAILED);
2246 1.21.2.3 bouyer return;
2247 1.21.2.3 bouyer
2248 1.21.2.3 bouyer /***** CBI Reset *****/
2249 1.21.2.3 bouyer case TSTATE_CBI_RESET1:
2250 1.21.2.3 bouyer if (err)
2251 1.21.2.3 bouyer printf("%s: CBI reset failed, %s\n",
2252 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2253 1.21.2.3 bouyer
2254 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
2255 1.21.2.3 bouyer sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2256 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_RESET2]);
2257 1.21.2.3 bouyer
2258 1.21.2.3 bouyer return;
2259 1.21.2.3 bouyer case TSTATE_CBI_RESET2:
2260 1.21.2.3 bouyer if (err) /* should not occur */
2261 1.21.2.3 bouyer printf("%s: CBI bulk-in stall clear failed, %s\n",
2262 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2263 1.21.2.3 bouyer /* no error recovery, otherwise we end up in a loop */
2264 1.21.2.3 bouyer
2265 1.21.2.3 bouyer umass_clear_endpoint_stall(sc,
2266 1.21.2.3 bouyer sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2267 1.21.2.3 bouyer sc->transfer_xfer[XFER_CBI_RESET3]);
2268 1.21.2.3 bouyer
2269 1.21.2.3 bouyer return;
2270 1.21.2.3 bouyer case TSTATE_CBI_RESET3:
2271 1.21.2.3 bouyer if (err) /* should not occur */
2272 1.21.2.3 bouyer printf("%s: CBI bulk-out stall clear failed, %s\n",
2273 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2274 1.21.2.3 bouyer /* no error recovery, otherwise we end up in a loop */
2275 1.21.2.3 bouyer
2276 1.21.2.3 bouyer sc->transfer_state = TSTATE_IDLE;
2277 1.21.2.3 bouyer if (sc->transfer_priv) {
2278 1.21.2.3 bouyer sc->transfer_cb(sc, sc->transfer_priv,
2279 1.21.2.3 bouyer sc->transfer_datalen,
2280 1.21.2.3 bouyer sc->transfer_status);
2281 1.21.2.3 bouyer }
2282 1.21.2.3 bouyer
2283 1.21.2.3 bouyer return;
2284 1.21.2.3 bouyer
2285 1.21.2.3 bouyer
2286 1.21.2.3 bouyer /***** Default *****/
2287 1.21.2.3 bouyer default:
2288 1.21.2.3 bouyer panic("%s: Unknown state %d\n",
2289 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->transfer_state);
2290 1.21.2.3 bouyer }
2291 1.21.2.3 bouyer }
2292 1.21.2.3 bouyer
2293 1.21.2.3 bouyer usbd_status
2294 1.21.2.3 bouyer umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
2295 1.21.2.3 bouyer {
2296 1.21.2.3 bouyer usbd_device_handle dev;
2297 1.21.2.3 bouyer usb_device_request_t req;
2298 1.21.2.3 bouyer usbd_status err;
2299 1.21.2.3 bouyer usb_interface_descriptor_t *id;
2300 1.21.2.3 bouyer
2301 1.21.2.3 bouyer *maxlun = 0; /* Default to 0. */
2302 1.21.2.3 bouyer
2303 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
2304 1.21.2.3 bouyer
2305 1.21.2.3 bouyer usbd_interface2device_handle(sc->iface, &dev);
2306 1.21.2.3 bouyer id = usbd_get_interface_descriptor(sc->iface);
2307 1.21.2.3 bouyer
2308 1.21.2.3 bouyer /* The Get Max Lun command is a class-specific request. */
2309 1.21.2.3 bouyer req.bmRequestType = UT_READ_CLASS_INTERFACE;
2310 1.21.2.3 bouyer req.bRequest = UR_BBB_GET_MAX_LUN;
2311 1.21.2.3 bouyer USETW(req.wValue, 0);
2312 1.21.2.3 bouyer USETW(req.wIndex, id->bInterfaceNumber);
2313 1.21.2.3 bouyer USETW(req.wLength, 1);
2314 1.21.2.3 bouyer
2315 1.21.2.3 bouyer err = usbd_do_request(dev, &req, maxlun);
2316 1.21.2.3 bouyer switch (err) {
2317 1.21.2.3 bouyer case USBD_NORMAL_COMPLETION:
2318 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
2319 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), *maxlun));
2320 1.21.2.3 bouyer break;
2321 1.21.2.3 bouyer
2322 1.21.2.3 bouyer case USBD_STALLED:
2323 1.21.2.3 bouyer /*
2324 1.21.2.3 bouyer * Device doesn't support Get Max Lun request.
2325 1.21.2.3 bouyer */
2326 1.21.2.3 bouyer err = USBD_NORMAL_COMPLETION;
2327 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
2328 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2329 1.21.2.3 bouyer break;
2330 1.21.2.3 bouyer
2331 1.21.2.3 bouyer case USBD_SHORT_XFER:
2332 1.21.2.3 bouyer /*
2333 1.21.2.3 bouyer * XXX This must mean Get Max Lun is not supported, too!
2334 1.21.2.3 bouyer */
2335 1.21.2.3 bouyer err = USBD_NORMAL_COMPLETION;
2336 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
2337 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2338 1.21.2.3 bouyer break;
2339 1.21.2.3 bouyer
2340 1.21.2.3 bouyer default:
2341 1.21.2.3 bouyer printf("%s: Get Max Lun failed: %s\n",
2342 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2343 1.21.2.3 bouyer /* XXX Should we port_reset the device? */
2344 1.21.2.3 bouyer break;
2345 1.21.2.3 bouyer }
2346 1.21.2.3 bouyer
2347 1.21.2.3 bouyer return (err);
2348 1.21.2.3 bouyer }
2349 1.21.2.3 bouyer
2350 1.21.2.3 bouyer
2351 1.21.2.3 bouyer
2352 1.21.2.3 bouyer #if defined(__FreeBSD__)
2353 1.21.2.3 bouyer /*
2354 1.21.2.3 bouyer * CAM specific functions (used by SCSI, UFI, 8070)
2355 1.21.2.3 bouyer */
2356 1.21.2.3 bouyer
2357 1.21.2.3 bouyer Static int
2358 1.21.2.3 bouyer umass_cam_attach_sim(void)
2359 1.21.2.3 bouyer {
2360 1.21.2.3 bouyer struct cam_devq *devq; /* Per device Queue */
2361 1.21.2.3 bouyer
2362 1.21.2.3 bouyer /* A HBA is attached to the CAM layer.
2363 1.21.2.3 bouyer *
2364 1.21.2.3 bouyer * The CAM layer will then after a while start probing for
2365 1.21.2.3 bouyer * devices on the bus. The number of devices is limitted to one.
2366 1.21.2.3 bouyer */
2367 1.21.2.3 bouyer
2368 1.21.2.3 bouyer /* SCSI transparent command set */
2369 1.21.2.3 bouyer
2370 1.21.2.3 bouyer devq = cam_simq_alloc(1 /*maximum openings*/);
2371 1.21.2.3 bouyer if (devq == NULL)
2372 1.21.2.3 bouyer return(ENOMEM);
2373 1.21.2.3 bouyer
2374 1.21.2.3 bouyer umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, DEVNAME,
2375 1.21.2.3 bouyer NULL /*priv*/, 0 /*unit number*/,
2376 1.21.2.3 bouyer 1 /*maximum device openings*/,
2377 1.21.2.3 bouyer 0 /*maximum tagged device openings*/,
2378 1.21.2.3 bouyer devq);
2379 1.21.2.3 bouyer if (umass_sim == NULL) {
2380 1.21.2.3 bouyer cam_simq_free(devq);
2381 1.21.2.3 bouyer return(ENOMEM);
2382 1.21.2.3 bouyer }
2383 1.21.2.3 bouyer
2384 1.21.2.3 bouyer if(xpt_bus_register(umass_sim, 0) != CAM_SUCCESS)
2385 1.21.2.3 bouyer return(ENOMEM);
2386 1.21.2.3 bouyer
2387 1.21.2.3 bouyer if (xpt_create_path(&umass_path, NULL, cam_sim_path(umass_sim),
2388 1.21.2.3 bouyer UMASS_SCSIID_HOST, 0)
2389 1.21.2.3 bouyer != CAM_REQ_CMP)
2390 1.21.2.3 bouyer return(ENOMEM);
2391 1.21.2.3 bouyer
2392 1.21.2.3 bouyer return(0);
2393 1.21.2.3 bouyer }
2394 1.21.2.3 bouyer
2395 1.21.2.3 bouyer #ifdef UMASS_DO_CAM_RESCAN
2396 1.21.2.3 bouyer /* this function is only used from umass_cam_rescan, so mention
2397 1.21.2.3 bouyer * prototype down here.
2398 1.21.2.3 bouyer */
2399 1.21.2.3 bouyer Static void umass_cam_rescan_callback(struct cam_periph *periph,union ccb *ccb);
2400 1.21.2.3 bouyer
2401 1.21.2.3 bouyer Static void
2402 1.21.2.3 bouyer umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2403 1.21.2.3 bouyer {
2404 1.21.2.3 bouyer #ifdef UMASS_DEBUG
2405 1.21.2.3 bouyer struct umass_softc *sc = devclass_get_softc(umass_devclass,
2406 1.21.2.3 bouyer ccb->ccb_h.target_id);
2407 1.21.2.3 bouyer
2408 1.21.2.3 bouyer if (ccb->ccb_h.status != CAM_REQ_CMP) {
2409 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan failed, 0x%04x\n",
2410 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2411 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2412 1.21.2.3 bouyer ccb->ccb_h.status));
2413 1.21.2.3 bouyer } else {
2414 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan succeeded, freeing resources.\n",
2415 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2416 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2417 1.21.2.3 bouyer }
2418 1.21.2.3 bouyer #endif
2419 1.21.2.3 bouyer
2420 1.21.2.3 bouyer xpt_free_path(ccb->ccb_h.path);
2421 1.21.2.3 bouyer free(ccb, M_USBDEV);
2422 1.21.2.3 bouyer }
2423 1.21.2.3 bouyer
2424 1.21.2.3 bouyer Static void
2425 1.21.2.3 bouyer umass_cam_rescan(struct umass_softc *sc)
2426 1.21.2.3 bouyer {
2427 1.21.2.3 bouyer struct cam_path *path;
2428 1.21.2.3 bouyer union ccb *ccb = malloc(sizeof(union ccb), M_USBDEV, M_WAITOK);
2429 1.21.2.3 bouyer
2430 1.21.2.3 bouyer memset(ccb, 0, sizeof(union ccb));
2431 1.21.2.3 bouyer
2432 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: scanning bus for new device %d\n",
2433 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), cam_sim_path(umass_sim),
2434 1.21.2.3 bouyer device_get_unit(sc->sc_dev), 0,
2435 1.21.2.3 bouyer device_get_unit(sc->sc_dev)));
2436 1.21.2.3 bouyer
2437 1.21.2.3 bouyer if (xpt_create_path(&path, xpt_periph, cam_sim_path(umass_sim),
2438 1.21.2.3 bouyer device_get_unit(sc->sc_dev), 0)
2439 1.21.2.3 bouyer != CAM_REQ_CMP)
2440 1.21.2.3 bouyer return;
2441 1.21.2.3 bouyer
2442 1.21.2.3 bouyer xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2443 1.21.2.3 bouyer ccb->ccb_h.func_code = XPT_SCAN_BUS;
2444 1.21.2.3 bouyer ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2445 1.21.2.3 bouyer ccb->crcn.flags = CAM_FLAG_NONE;
2446 1.21.2.3 bouyer xpt_action(ccb);
2447 1.21.2.3 bouyer
2448 1.21.2.3 bouyer /* The scan is in progress now. */
2449 1.21.2.3 bouyer }
2450 1.21.2.3 bouyer #endif
2451 1.21.2.3 bouyer
2452 1.21.2.3 bouyer Static int
2453 1.21.2.3 bouyer umass_cam_attach(struct umass_softc *sc)
2454 1.21.2.3 bouyer {
2455 1.21.2.3 bouyer /* SIM already attached at module load. The device is a target on the
2456 1.21.2.3 bouyer * one SIM we registered: target device_get_unit(self).
2457 1.21.2.3 bouyer */
2458 1.21.2.3 bouyer
2459 1.21.2.3 bouyer /* The artificial limit UMASS_SCSIID_MAX is there because CAM expects
2460 1.21.2.3 bouyer * a limit to the number of targets that are present on a SIM.
2461 1.21.2.3 bouyer */
2462 1.21.2.3 bouyer if (device_get_unit(sc->sc_dev) > UMASS_SCSIID_MAX) {
2463 1.21.2.3 bouyer printf("%s: Increase UMASS_SCSIID_MAX (currently %d) in %s "
2464 1.21.2.3 bouyer "and try again.\n", USBDEVNAME(sc->sc_dev),
2465 1.21.2.3 bouyer UMASS_SCSIID_MAX, __FILE__);
2466 1.21.2.3 bouyer return(1);
2467 1.21.2.3 bouyer }
2468 1.21.2.3 bouyer
2469 1.21.2.3 bouyer #ifdef UMASS_DO_CAM_RESCAN
2470 1.21.2.3 bouyer if (!cold) {
2471 1.21.2.3 bouyer /* Notify CAM of the new device. Any failure is benign, as the
2472 1.21.2.3 bouyer * user can still do it by hand (camcontrol rescan <busno>).
2473 1.21.2.3 bouyer * Only do this if we are not booting, because CAM does a scan
2474 1.21.2.3 bouyer * after booting has completed, when interrupts have been
2475 1.21.2.3 bouyer * enabled.
2476 1.21.2.3 bouyer */
2477 1.21.2.3 bouyer umass_cam_rescan(sc);
2478 1.21.2.3 bouyer }
2479 1.21.2.3 bouyer #endif
2480 1.21.2.3 bouyer
2481 1.21.2.3 bouyer return(0); /* always succesful */
2482 1.21.2.3 bouyer }
2483 1.21.2.3 bouyer
2484 1.21.2.3 bouyer /* umass_cam_detach
2485 1.21.2.3 bouyer * detach from the CAM layer
2486 1.21.2.3 bouyer */
2487 1.21.2.3 bouyer
2488 1.21.2.3 bouyer Static int
2489 1.21.2.3 bouyer umass_cam_detach_sim(void)
2490 1.21.2.3 bouyer {
2491 1.21.2.3 bouyer if (umass_sim)
2492 1.21.2.3 bouyer return(EBUSY); /* XXX CAM can't handle disappearing SIMs yet */
2493 1.21.2.3 bouyer
2494 1.21.2.3 bouyer if (umass_path) {
2495 1.21.2.3 bouyer /* XXX do we need to send an asynchroneous event for the SIM?
2496 1.21.2.3 bouyer xpt_async(AC_LOST_DEVICE, umass_path, NULL);
2497 1.21.2.3 bouyer */
2498 1.21.2.3 bouyer xpt_free_path(umass_path);
2499 1.21.2.3 bouyer umass_path = NULL;
2500 1.21.2.3 bouyer }
2501 1.21.2.3 bouyer
2502 1.21.2.3 bouyer if (umass_sim) {
2503 1.21.2.3 bouyer if (xpt_bus_deregister(cam_sim_path(umass_sim)))
2504 1.21.2.3 bouyer cam_sim_free(umass_sim, /*free_devq*/TRUE);
2505 1.21.2.3 bouyer else
2506 1.21.2.3 bouyer return(EBUSY);
2507 1.21.2.3 bouyer
2508 1.21.2.3 bouyer umass_sim = NULL;
2509 1.21.2.3 bouyer }
2510 1.21.2.3 bouyer
2511 1.21.2.3 bouyer return(0);
2512 1.21.2.3 bouyer }
2513 1.21.2.3 bouyer
2514 1.21.2.3 bouyer Static int
2515 1.21.2.3 bouyer umass_cam_detach(struct umass_softc *sc)
2516 1.21.2.3 bouyer {
2517 1.21.2.3 bouyer struct cam_path *path;
2518 1.21.2.3 bouyer
2519 1.21.2.3 bouyer /* detach of sim not done until module unload */
2520 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: losing CAM device entry\n",
2521 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2522 1.21.2.3 bouyer
2523 1.21.2.3 bouyer if (xpt_create_path(&path, NULL, cam_sim_path(umass_sim),
2524 1.21.2.3 bouyer device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD)
2525 1.21.2.3 bouyer != CAM_REQ_CMP)
2526 1.21.2.3 bouyer return(ENOMEM);
2527 1.21.2.3 bouyer xpt_async(AC_LOST_DEVICE, path, NULL);
2528 1.21.2.3 bouyer xpt_free_path(path);
2529 1.21.2.3 bouyer
2530 1.21.2.3 bouyer return(0);
2531 1.21.2.3 bouyer }
2532 1.21.2.3 bouyer
2533 1.21.2.3 bouyer
2534 1.21.2.3 bouyer
2535 1.21.2.3 bouyer /* umass_cam_action
2536 1.21.2.3 bouyer * CAM requests for action come through here
2537 1.21.2.3 bouyer */
2538 1.21.2.3 bouyer
2539 1.21.2.3 bouyer Static void
2540 1.21.2.3 bouyer umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2541 1.21.2.3 bouyer {
2542 1.21.2.3 bouyer struct umass_softc *sc = devclass_get_softc(umass_devclass,
2543 1.21.2.3 bouyer ccb->ccb_h.target_id);
2544 1.21.2.3 bouyer
2545 1.21.2.3 bouyer /* The softc is still there, but marked as going away. umass_cam_detach
2546 1.21.2.3 bouyer * has not yet notified CAM of the lost device however.
2547 1.21.2.3 bouyer */
2548 1.21.2.3 bouyer if (sc && sc->sc_dying) {
2549 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2550 1.21.2.3 bouyer "Invalid target (gone)\n",
2551 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2552 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2553 1.21.2.3 bouyer ccb->ccb_h.func_code));
2554 1.21.2.3 bouyer ccb->ccb_h.status = CAM_TID_INVALID;
2555 1.21.2.3 bouyer xpt_done(ccb);
2556 1.21.2.3 bouyer return;
2557 1.21.2.3 bouyer }
2558 1.21.2.3 bouyer
2559 1.21.2.3 bouyer /* Verify, depending on the operation to perform, that we either got a
2560 1.21.2.3 bouyer * valid sc, because an existing target was referenced, or otherwise
2561 1.21.2.3 bouyer * the SIM is addressed.
2562 1.21.2.3 bouyer *
2563 1.21.2.3 bouyer * This avoids bombing out at a printf and does give the CAM layer some
2564 1.21.2.3 bouyer * sensible feedback on errors.
2565 1.21.2.3 bouyer */
2566 1.21.2.3 bouyer switch (ccb->ccb_h.func_code) {
2567 1.21.2.3 bouyer case XPT_SCSI_IO:
2568 1.21.2.3 bouyer case XPT_RESET_DEV:
2569 1.21.2.3 bouyer case XPT_GET_TRAN_SETTINGS:
2570 1.21.2.3 bouyer case XPT_SET_TRAN_SETTINGS:
2571 1.21.2.3 bouyer case XPT_CALC_GEOMETRY:
2572 1.21.2.3 bouyer /* the opcodes requiring a target. These should never occur. */
2573 1.21.2.3 bouyer if (sc == NULL) {
2574 1.21.2.3 bouyer printf("%s:%d:%d:%d:func_code 0x%04x: "
2575 1.21.2.3 bouyer "Invalid target\n",
2576 1.21.2.3 bouyer DEVNAME_SIM, UMASS_SCSI_BUS,
2577 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2578 1.21.2.3 bouyer ccb->ccb_h.func_code);
2579 1.21.2.3 bouyer
2580 1.21.2.3 bouyer ccb->ccb_h.status = CAM_TID_INVALID;
2581 1.21.2.3 bouyer xpt_done(ccb);
2582 1.21.2.3 bouyer return;
2583 1.21.2.3 bouyer }
2584 1.21.2.3 bouyer break;
2585 1.21.2.3 bouyer case XPT_PATH_INQ:
2586 1.21.2.3 bouyer case XPT_NOOP:
2587 1.21.2.3 bouyer /* The opcodes sometimes aimed at a target (sc is valid),
2588 1.21.2.3 bouyer * sometimes aimed at the SIM (sc is invalid and target is
2589 1.21.2.3 bouyer * CAM_TARGET_WILDCARD)
2590 1.21.2.3 bouyer */
2591 1.21.2.3 bouyer if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2592 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2593 1.21.2.3 bouyer "Invalid target\n",
2594 1.21.2.3 bouyer DEVNAME_SIM, UMASS_SCSI_BUS,
2595 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2596 1.21.2.3 bouyer ccb->ccb_h.func_code));
2597 1.21.2.3 bouyer
2598 1.21.2.3 bouyer ccb->ccb_h.status = CAM_TID_INVALID;
2599 1.21.2.3 bouyer xpt_done(ccb);
2600 1.21.2.3 bouyer return;
2601 1.21.2.3 bouyer }
2602 1.21.2.3 bouyer break;
2603 1.21.2.3 bouyer default:
2604 1.21.2.3 bouyer /* XXX Hm, we should check the input parameters */
2605 1.21.2.3 bouyer }
2606 1.21.2.3 bouyer
2607 1.21.2.3 bouyer /* Perform the requested action */
2608 1.21.2.3 bouyer switch (ccb->ccb_h.func_code) {
2609 1.21.2.3 bouyer case XPT_SCSI_IO:
2610 1.21.2.3 bouyer {
2611 1.21.2.3 bouyer struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2612 1.21.2.3 bouyer int dir;
2613 1.21.2.3 bouyer unsigned char *cmd;
2614 1.21.2.3 bouyer int cmdlen;
2615 1.21.2.3 bouyer
2616 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2617 1.21.2.3 bouyer "cmd: 0x%02x, flags: 0x%02x, "
2618 1.21.2.3 bouyer "%db cmd/%db data/%db sense\n",
2619 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2620 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2621 1.21.2.3 bouyer csio->cdb_io.cdb_bytes[0],
2622 1.21.2.3 bouyer ccb->ccb_h.flags & CAM_DIR_MASK,
2623 1.21.2.3 bouyer csio->cdb_len, csio->dxfer_len,
2624 1.21.2.3 bouyer csio->sense_len));
2625 1.21.2.3 bouyer
2626 1.21.2.3 bouyer /* clear the end of the buffer to make sure we don't send out
2627 1.21.2.3 bouyer * garbage.
2628 1.21.2.3 bouyer */
2629 1.21.2.3 bouyer DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2630 1.21.2.3 bouyer == CAM_DIR_OUT)
2631 1.21.2.3 bouyer umass_dump_buffer(sc, csio->data_ptr,
2632 1.21.2.3 bouyer csio->dxfer_len, 48));
2633 1.21.2.3 bouyer
2634 1.21.2.3 bouyer if (sc->transfer_state != TSTATE_IDLE) {
2635 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2636 1.21.2.3 bouyer "I/O requested while busy (state %d, %s)\n",
2637 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2638 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2639 1.21.2.3 bouyer sc->transfer_state,states[sc->transfer_state]));
2640 1.21.2.3 bouyer ccb->ccb_h.status = CAM_SCSI_BUSY;
2641 1.21.2.3 bouyer xpt_done(ccb);
2642 1.21.2.3 bouyer return;
2643 1.21.2.3 bouyer }
2644 1.21.2.3 bouyer
2645 1.21.2.3 bouyer switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2646 1.21.2.3 bouyer case CAM_DIR_IN:
2647 1.21.2.3 bouyer dir = DIR_IN;
2648 1.21.2.3 bouyer break;
2649 1.21.2.3 bouyer case CAM_DIR_OUT:
2650 1.21.2.3 bouyer dir = DIR_OUT;
2651 1.21.2.3 bouyer break;
2652 1.21.2.3 bouyer default:
2653 1.21.2.3 bouyer dir = DIR_NONE;
2654 1.21.2.3 bouyer }
2655 1.21.2.3 bouyer
2656 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2657 1.21.2.3 bouyer if (sc->transform(sc, csio->cdb_io.cdb_bytes, csio->cdb_len,
2658 1.21.2.3 bouyer &cmd, &cmdlen)) {
2659 1.21.2.3 bouyer sc->transfer(sc, ccb->ccb_h.target_lun, cmd, cmdlen,
2660 1.21.2.3 bouyer csio->data_ptr,
2661 1.21.2.3 bouyer csio->dxfer_len, dir,
2662 1.21.2.3 bouyer umass_cam_cb, (void *) ccb);
2663 1.21.2.3 bouyer } else {
2664 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_INVALID;
2665 1.21.2.3 bouyer xpt_done(ccb);
2666 1.21.2.3 bouyer }
2667 1.21.2.3 bouyer
2668 1.21.2.3 bouyer break;
2669 1.21.2.3 bouyer }
2670 1.21.2.3 bouyer case XPT_PATH_INQ:
2671 1.21.2.3 bouyer {
2672 1.21.2.3 bouyer struct ccb_pathinq *cpi = &ccb->cpi;
2673 1.21.2.3 bouyer
2674 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
2675 1.21.2.3 bouyer (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2676 1.21.2.3 bouyer UMASS_SCSI_BUS,
2677 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2678 1.21.2.3 bouyer
2679 1.21.2.3 bouyer /* host specific information */
2680 1.21.2.3 bouyer cpi->version_num = 1;
2681 1.21.2.3 bouyer cpi->hba_inquiry = 0;
2682 1.21.2.3 bouyer cpi->target_sprt = 0;
2683 1.21.2.3 bouyer cpi->hba_misc = 0;
2684 1.21.2.3 bouyer cpi->hba_eng_cnt = 0;
2685 1.21.2.3 bouyer cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2686 1.21.2.3 bouyer cpi->max_lun = 0; /* no LUN's supported */
2687 1.21.2.3 bouyer cpi->initiator_id = UMASS_SCSIID_HOST;
2688 1.21.2.3 bouyer strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2689 1.21.2.3 bouyer strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2690 1.21.2.3 bouyer strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2691 1.21.2.3 bouyer cpi->unit_number = cam_sim_unit(sim);
2692 1.21.2.3 bouyer cpi->bus_id = UMASS_SCSI_BUS;
2693 1.21.2.3 bouyer if (sc) {
2694 1.21.2.3 bouyer cpi->base_transfer_speed = sc->transfer_speed;
2695 1.21.2.3 bouyer cpi->max_lun = sc->maxlun;
2696 1.21.2.3 bouyer }
2697 1.21.2.3 bouyer
2698 1.21.2.3 bouyer cpi->ccb_h.status = CAM_REQ_CMP;
2699 1.21.2.3 bouyer xpt_done(ccb);
2700 1.21.2.3 bouyer break;
2701 1.21.2.3 bouyer }
2702 1.21.2.3 bouyer case XPT_RESET_DEV:
2703 1.21.2.3 bouyer {
2704 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
2705 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2706 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2707 1.21.2.3 bouyer
2708 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_INPROG;
2709 1.21.2.3 bouyer umass_reset(sc, umass_cam_cb, (void *) ccb);
2710 1.21.2.3 bouyer break;
2711 1.21.2.3 bouyer }
2712 1.21.2.3 bouyer case XPT_GET_TRAN_SETTINGS:
2713 1.21.2.3 bouyer {
2714 1.21.2.3 bouyer struct ccb_trans_settings *cts = &ccb->cts;
2715 1.21.2.3 bouyer
2716 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2717 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2718 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2719 1.21.2.3 bouyer
2720 1.21.2.3 bouyer cts->valid = 0;
2721 1.21.2.3 bouyer cts->flags = 0; /* no disconnection, tagging */
2722 1.21.2.3 bouyer
2723 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2724 1.21.2.3 bouyer xpt_done(ccb);
2725 1.21.2.3 bouyer break;
2726 1.21.2.3 bouyer }
2727 1.21.2.3 bouyer case XPT_SET_TRAN_SETTINGS:
2728 1.21.2.3 bouyer {
2729 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2730 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2731 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2732 1.21.2.3 bouyer
2733 1.21.2.3 bouyer ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2734 1.21.2.3 bouyer xpt_done(ccb);
2735 1.21.2.3 bouyer break;
2736 1.21.2.3 bouyer }
2737 1.21.2.3 bouyer case XPT_CALC_GEOMETRY:
2738 1.21.2.3 bouyer {
2739 1.21.2.3 bouyer struct ccb_calc_geometry *ccg = &ccb->ccg;
2740 1.21.2.3 bouyer
2741 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2742 1.21.2.3 bouyer "Volume size = %d\n",
2743 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2744 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2745 1.21.2.3 bouyer ccg->volume_size));
2746 1.21.2.3 bouyer
2747 1.21.2.3 bouyer /* XXX We should probably ask the drive for the details
2748 1.21.2.3 bouyer * instead of cluching them up ourselves
2749 1.21.2.3 bouyer */
2750 1.21.2.3 bouyer if (sc->drive == ZIP_100) {
2751 1.21.2.3 bouyer ccg->heads = 64;
2752 1.21.2.3 bouyer ccg->secs_per_track = 32;
2753 1.21.2.3 bouyer ccg->cylinders = ccg->volume_size / ccg->heads
2754 1.21.2.3 bouyer / ccg->secs_per_track;
2755 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2756 1.21.2.3 bouyer break;
2757 1.21.2.3 bouyer } else if (sc->proto & PROTO_UFI) {
2758 1.21.2.3 bouyer ccg->heads = 2;
2759 1.21.2.3 bouyer if (ccg->volume_size == 2880)
2760 1.21.2.3 bouyer ccg->secs_per_track = 18;
2761 1.21.2.3 bouyer else
2762 1.21.2.3 bouyer ccg->secs_per_track = 9;
2763 1.21.2.3 bouyer ccg->cylinders = 80;
2764 1.21.2.3 bouyer break;
2765 1.21.2.3 bouyer } else {
2766 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2767 1.21.2.3 bouyer }
2768 1.21.2.3 bouyer
2769 1.21.2.3 bouyer xpt_done(ccb);
2770 1.21.2.3 bouyer break;
2771 1.21.2.3 bouyer }
2772 1.21.2.3 bouyer case XPT_NOOP:
2773 1.21.2.3 bouyer {
2774 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
2775 1.21.2.3 bouyer (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2776 1.21.2.3 bouyer UMASS_SCSI_BUS,
2777 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2778 1.21.2.3 bouyer
2779 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2780 1.21.2.3 bouyer xpt_done(ccb);
2781 1.21.2.3 bouyer break;
2782 1.21.2.3 bouyer }
2783 1.21.2.3 bouyer default:
2784 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2785 1.21.2.3 bouyer "Not implemented\n",
2786 1.21.2.3 bouyer (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2787 1.21.2.3 bouyer UMASS_SCSI_BUS,
2788 1.21.2.3 bouyer ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2789 1.21.2.3 bouyer ccb->ccb_h.func_code));
2790 1.21.2.3 bouyer
2791 1.21.2.3 bouyer ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2792 1.21.2.3 bouyer xpt_done(ccb);
2793 1.21.2.3 bouyer break;
2794 1.21.2.3 bouyer }
2795 1.21.2.3 bouyer }
2796 1.21.2.3 bouyer
2797 1.21.2.3 bouyer /* umass_cam_poll
2798 1.21.2.3 bouyer * all requests are handled through umass_cam_action, requests
2799 1.21.2.3 bouyer * are never pending. So, nothing to do here.
2800 1.21.2.3 bouyer */
2801 1.21.2.3 bouyer Static void
2802 1.21.2.3 bouyer umass_cam_poll(struct cam_sim *sim)
2803 1.21.2.3 bouyer {
2804 1.21.2.3 bouyer #ifdef UMASS_DEBUG
2805 1.21.2.3 bouyer struct umass_softc *sc = (struct umass_softc *) sim->softc;
2806 1.21.2.3 bouyer
2807 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
2808 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev)));
2809 1.21.2.3 bouyer #endif
2810 1.21.2.3 bouyer
2811 1.21.2.3 bouyer /* nop */
2812 1.21.2.3 bouyer }
2813 1.21.2.3 bouyer
2814 1.21.2.3 bouyer
2815 1.21.2.3 bouyer /* umass_cam_cb
2816 1.21.2.3 bouyer * finalise a completed CAM command
2817 1.21.2.3 bouyer */
2818 1.21.2.3 bouyer
2819 1.21.2.3 bouyer Static void
2820 1.21.2.3 bouyer umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
2821 1.21.2.3 bouyer {
2822 1.21.2.3 bouyer union ccb *ccb = (union ccb *) priv;
2823 1.21.2.3 bouyer struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2824 1.21.2.3 bouyer
2825 1.21.2.3 bouyer csio->resid = residue;
2826 1.21.2.3 bouyer
2827 1.21.2.3 bouyer switch (status) {
2828 1.21.2.3 bouyer case STATUS_CMD_OK:
2829 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2830 1.21.2.3 bouyer xpt_done(ccb);
2831 1.21.2.3 bouyer break;
2832 1.21.2.3 bouyer
2833 1.21.2.3 bouyer case STATUS_CMD_UNKNOWN:
2834 1.21.2.3 bouyer case STATUS_CMD_FAILED:
2835 1.21.2.3 bouyer switch (ccb->ccb_h.func_code) {
2836 1.21.2.3 bouyer case XPT_SCSI_IO:
2837 1.21.2.3 bouyer {
2838 1.21.2.3 bouyer unsigned char *cmd;
2839 1.21.2.3 bouyer int cmdlen;
2840 1.21.2.3 bouyer
2841 1.21.2.3 bouyer /* fetch sense data */
2842 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
2843 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev),
2844 1.21.2.3 bouyer sc->cam_scsi_sense.length));
2845 1.21.2.3 bouyer
2846 1.21.2.3 bouyer sc->cam_scsi_sense.length = csio->sense_len;
2847 1.21.2.3 bouyer
2848 1.21.2.3 bouyer if (sc->transform(sc, (char *) &sc->cam_scsi_sense,
2849 1.21.2.3 bouyer sizeof(sc->cam_scsi_sense),
2850 1.21.2.3 bouyer &cmd, &cmdlen)) {
2851 1.21.2.3 bouyer sc->transfer(sc, ccb->ccb_h.target_lun,
2852 1.21.2.3 bouyer cmd, cmdlen,
2853 1.21.2.3 bouyer &csio->sense_data,
2854 1.21.2.3 bouyer csio->sense_len, DIR_IN,
2855 1.21.2.3 bouyer umass_cam_sense_cb, (void *) ccb);
2856 1.21.2.3 bouyer } else {
2857 1.21.2.3 bouyer #ifdef UMASS_DEBUG
2858 1.21.2.3 bouyer panic("transform(REQUEST_SENSE) failed\n");
2859 1.21.2.3 bouyer #else
2860 1.21.2.3 bouyer csio->resid = sc->transfer_datalen;
2861 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2862 1.21.2.3 bouyer xpt_done(ccb);
2863 1.21.2.3 bouyer #endif
2864 1.21.2.3 bouyer }
2865 1.21.2.3 bouyer break;
2866 1.21.2.3 bouyer }
2867 1.21.2.3 bouyer case XPT_RESET_DEV: /* Reset failed */
2868 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2869 1.21.2.3 bouyer xpt_done(ccb);
2870 1.21.2.3 bouyer break;
2871 1.21.2.3 bouyer default:
2872 1.21.2.3 bouyer panic("umass_cam_cb called for func_code %d\n",
2873 1.21.2.3 bouyer ccb->ccb_h.func_code);
2874 1.21.2.3 bouyer }
2875 1.21.2.3 bouyer break;
2876 1.21.2.3 bouyer
2877 1.21.2.3 bouyer case STATUS_WIRE_FAILED:
2878 1.21.2.3 bouyer /* the wire protocol failed and will have recovered
2879 1.21.2.3 bouyer * (hopefully). We return an error to CAM and let CAM retry
2880 1.21.2.3 bouyer * the command if necessary.
2881 1.21.2.3 bouyer */
2882 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2883 1.21.2.3 bouyer xpt_done(ccb);
2884 1.21.2.3 bouyer break;
2885 1.21.2.3 bouyer
2886 1.21.2.3 bouyer default:
2887 1.21.2.3 bouyer panic("%s: Unknown status %d in umass_cam_cb\n",
2888 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), status);
2889 1.21.2.3 bouyer }
2890 1.21.2.3 bouyer }
2891 1.21.2.3 bouyer
2892 1.21.2.3 bouyer /* Finalise a completed autosense operation
2893 1.21.2.3 bouyer */
2894 1.21.2.3 bouyer Static void
2895 1.21.2.3 bouyer umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
2896 1.21.2.3 bouyer {
2897 1.21.2.3 bouyer union ccb *ccb = (union ccb *) priv;
2898 1.21.2.3 bouyer struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2899 1.21.2.3 bouyer
2900 1.21.2.3 bouyer switch (status) {
2901 1.21.2.3 bouyer case STATUS_CMD_OK:
2902 1.21.2.3 bouyer case STATUS_CMD_UNKNOWN:
2903 1.21.2.3 bouyer /* Getting sense data succeeded. The length of the sense data
2904 1.21.2.3 bouyer * is not returned in any way. The sense data itself contains
2905 1.21.2.3 bouyer * the length of the sense data that is valid.
2906 1.21.2.3 bouyer */
2907 1.21.2.3 bouyer if (sc->quirks & RS_NO_CLEAR_UA
2908 1.21.2.3 bouyer && csio->cdb_io.cdb_bytes[0] == INQUIRY
2909 1.21.2.3 bouyer && (csio->sense_data.flags & SSD_KEY)
2910 1.21.2.3 bouyer == SSD_KEY_UNIT_ATTENTION) {
2911 1.21.2.3 bouyer /* Ignore unit attention errors in the case where
2912 1.21.2.3 bouyer * the Unit Attention state is not cleared on
2913 1.21.2.3 bouyer * REQUEST SENSE. They will appear again at the next
2914 1.21.2.3 bouyer * command.
2915 1.21.2.3 bouyer */
2916 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2917 1.21.2.3 bouyer } else if ((csio->sense_data.flags & SSD_KEY)
2918 1.21.2.3 bouyer == SSD_KEY_NO_SENSE) {
2919 1.21.2.3 bouyer /* No problem after all (in the case of CBI without
2920 1.21.2.3 bouyer * CCI)
2921 1.21.2.3 bouyer */
2922 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_CMP;
2923 1.21.2.3 bouyer } else {
2924 1.21.2.3 bouyer ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2925 1.21.2.3 bouyer | CAM_AUTOSNS_VALID;
2926 1.21.2.3 bouyer csio->scsi_status = SCSI_STATUS_CHECK_COND;
2927 1.21.2.3 bouyer }
2928 1.21.2.3 bouyer xpt_done(ccb);
2929 1.21.2.3 bouyer break;
2930 1.21.2.3 bouyer
2931 1.21.2.3 bouyer default:
2932 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
2933 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), status));
2934 1.21.2.3 bouyer ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2935 1.21.2.3 bouyer xpt_done(ccb);
2936 1.21.2.3 bouyer }
2937 1.21.2.3 bouyer }
2938 1.21.2.3 bouyer
2939 1.21.2.3 bouyer
2940 1.21.2.3 bouyer Static int
2941 1.21.2.3 bouyer umass_driver_load(module_t mod, int what, void *arg)
2942 1.21.2.3 bouyer {
2943 1.21.2.3 bouyer int err;
2944 1.21.2.3 bouyer
2945 1.21.2.3 bouyer switch (what) {
2946 1.21.2.3 bouyer case MOD_UNLOAD:
2947 1.21.2.3 bouyer err = umass_cam_detach_sim();
2948 1.21.2.3 bouyer if (err)
2949 1.21.2.3 bouyer return(err);
2950 1.21.2.3 bouyer return(usbd_driver_load(mod, what, arg));
2951 1.21.2.3 bouyer case MOD_LOAD:
2952 1.21.2.3 bouyer /* We don't attach to CAM at this point, because it will try
2953 1.21.2.3 bouyer * and malloc memory for it. This is not possible when the
2954 1.21.2.3 bouyer * boot loader loads umass as a module before the kernel
2955 1.21.2.3 bouyer * has been bootstrapped.
2956 1.21.2.3 bouyer */
2957 1.21.2.3 bouyer default:
2958 1.21.2.3 bouyer return(usbd_driver_load(mod, what, arg));
2959 1.21.2.3 bouyer }
2960 1.21.2.3 bouyer }
2961 1.21.2.3 bouyer
2962 1.21.2.3 bouyer
2963 1.21.2.3 bouyer
2964 1.21.2.3 bouyer /* (even the comment is missing) */
2965 1.21.2.3 bouyer
2966 1.21.2.3 bouyer DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
2967 1.21.2.3 bouyer
2968 1.21.2.3 bouyer
2969 1.21.2.3 bouyer /*
2970 1.21.2.3 bouyer * SCSI specific functions
2971 1.21.2.3 bouyer */
2972 1.21.2.3 bouyer
2973 1.21.2.3 bouyer Static int
2974 1.21.2.3 bouyer umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2975 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen)
2976 1.21.2.3 bouyer {
2977 1.21.2.3 bouyer *rcmd = cmd; /* trivial copy */
2978 1.21.2.3 bouyer *rcmdlen = cmdlen;
2979 1.21.2.3 bouyer
2980 1.21.2.3 bouyer switch (cmd[0]) {
2981 1.21.2.3 bouyer case TEST_UNIT_READY:
2982 1.21.2.3 bouyer if (sc->quirks & NO_TEST_UNIT_READY) {
2983 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
2984 1.21.2.3 bouyer "to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
2985 1.21.2.3 bouyer cmd[0] = START_STOP_UNIT;
2986 1.21.2.3 bouyer cmd[4] = SSS_START;
2987 1.21.2.3 bouyer }
2988 1.21.2.3 bouyer break;
2989 1.21.2.3 bouyer }
2990 1.21.2.3 bouyer
2991 1.21.2.3 bouyer return 1; /* success */
2992 1.21.2.3 bouyer }
2993 1.21.2.3 bouyer
2994 1.21.2.3 bouyer /*
2995 1.21.2.3 bouyer * UFI specific functions
2996 1.21.2.3 bouyer */
2997 1.21.2.3 bouyer
2998 1.21.2.3 bouyer Static int
2999 1.21.2.3 bouyer umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3000 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen)
3001 1.21.2.3 bouyer {
3002 1.21.2.3 bouyer *rcmd = cmd;
3003 1.21.2.3 bouyer /* A UFI command is always 12 bytes in length */
3004 1.21.2.3 bouyer /* XXX cmd[(cmdlen+1)..12] contains garbage */
3005 1.21.2.3 bouyer *rcmdlen = 12;
3006 1.21.2.3 bouyer
3007 1.21.2.3 bouyer switch (cmd[0]) {
3008 1.21.2.3 bouyer case TEST_UNIT_READY:
3009 1.21.2.3 bouyer if (sc->quirks & NO_TEST_UNIT_READY) {
3010 1.21.2.3 bouyer DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3011 1.21.2.3 bouyer "to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
3012 1.21.2.3 bouyer cmd[0] = START_STOP_UNIT;
3013 1.21.2.3 bouyer cmd[4] = SSS_START;
3014 1.21.2.3 bouyer }
3015 1.21.2.3 bouyer return 1;
3016 1.21.2.3 bouyer case INQUIRY:
3017 1.21.2.3 bouyer case START_STOP_UNIT:
3018 1.21.2.3 bouyer case MODE_SENSE:
3019 1.21.2.3 bouyer case PREVENT_ALLOW:
3020 1.21.2.3 bouyer case READ_10:
3021 1.21.2.3 bouyer case READ_12:
3022 1.21.2.3 bouyer case READ_CAPACITY:
3023 1.21.2.3 bouyer case REQUEST_SENSE:
3024 1.21.2.3 bouyer case REZERO_UNIT:
3025 1.21.2.3 bouyer case POSITION_TO_ELEMENT: /* SEEK_10 */
3026 1.21.2.3 bouyer case SEND_DIAGNOSTIC:
3027 1.21.2.3 bouyer case WRITE_10:
3028 1.21.2.3 bouyer case WRITE_12:
3029 1.21.2.3 bouyer /* FORMAT_UNIT */
3030 1.21.2.3 bouyer /* MODE_SELECT */
3031 1.21.2.3 bouyer /* READ_FORMAT_CAPACITY */
3032 1.21.2.3 bouyer /* VERIFY */
3033 1.21.2.3 bouyer /* WRITE_AND_VERIFY */
3034 1.21.2.3 bouyer return 1; /* success */
3035 1.21.2.3 bouyer default:
3036 1.21.2.3 bouyer return 0; /* success */
3037 1.21.2.3 bouyer }
3038 1.21.2.3 bouyer }
3039 1.21.2.3 bouyer
3040 1.21.2.3 bouyer /*
3041 1.21.2.3 bouyer * 8070 specific functions
3042 1.21.2.3 bouyer */
3043 1.21.2.3 bouyer Static int
3044 1.21.2.3 bouyer umass_8070_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3045 1.21.2.3 bouyer unsigned char **rcmd, int *rcmdlen)
3046 1.21.2.3 bouyer {
3047 1.21.2.3 bouyer return 0; /* failure */
3048 1.21.2.3 bouyer }
3049 1.21.2.3 bouyer
3050 1.21.2.3 bouyer #endif /* __FreeBSD__ */
3051 1.21.2.3 bouyer
3052 1.21.2.3 bouyer
3053 1.21.2.3 bouyer #ifdef UMASS_DEBUG
3054 1.21.2.3 bouyer Static void
3055 1.21.2.3 bouyer umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3056 1.21.2.3 bouyer {
3057 1.21.2.3 bouyer int clen = cbw->bCDBLength;
3058 1.21.2.3 bouyer int dlen = UGETDW(cbw->dCBWDataTransferLength);
3059 1.21.2.3 bouyer u_int8_t *c = cbw->CBWCDB;
3060 1.21.2.3 bouyer int tag = UGETDW(cbw->dCBWTag);
3061 1.21.2.3 bouyer int flags = cbw->bCBWFlags;
3062 1.21.2.3 bouyer
3063 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3064 1.21.2.3 bouyer "(0x%02x%02x%02x%02x%02x%02x%s), "
3065 1.21.2.3 bouyer "data = %d bytes, dir = %s\n",
3066 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), tag, clen,
3067 1.21.2.3 bouyer c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3068 1.21.2.3 bouyer dlen, (flags == CBWFLAGS_IN? "in":
3069 1.21.2.3 bouyer (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3070 1.21.2.3 bouyer }
3071 1.21.2.3 bouyer
3072 1.21.2.3 bouyer Static void
3073 1.21.2.3 bouyer umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3074 1.21.2.3 bouyer {
3075 1.21.2.3 bouyer int sig = UGETDW(csw->dCSWSignature);
3076 1.21.2.3 bouyer int tag = UGETW(csw->dCSWTag);
3077 1.21.2.3 bouyer int res = UGETDW(csw->dCSWDataResidue);
3078 1.21.2.3 bouyer int status = csw->bCSWStatus;
3079 1.21.2.3 bouyer
3080 1.21.2.3 bouyer DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3081 1.21.2.3 bouyer "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
3082 1.21.2.3 bouyer tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3083 1.21.2.3 bouyer tag, res,
3084 1.21.2.3 bouyer status, (status == CSWSTATUS_GOOD? "good":
3085 1.21.2.3 bouyer (status == CSWSTATUS_FAILED? "failed":
3086 1.21.2.3 bouyer (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3087 1.21.2.3 bouyer }
3088 1.21.2.3 bouyer
3089 1.21.2.3 bouyer Static void
3090 1.21.2.3 bouyer umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3091 1.21.2.3 bouyer int printlen)
3092 1.21.2.3 bouyer {
3093 1.21.2.3 bouyer int i, j;
3094 1.21.2.3 bouyer char s1[40];
3095 1.21.2.3 bouyer char s2[40];
3096 1.21.2.3 bouyer char s3[5];
3097 1.21.2.3 bouyer
3098 1.21.2.3 bouyer s1[0] = '\0';
3099 1.21.2.3 bouyer s3[0] = '\0';
3100 1.21.2.3 bouyer
3101 1.21.2.3 bouyer sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3102 1.21.2.3 bouyer for (i = 0; i < buflen && i < printlen; i++) {
3103 1.21.2.3 bouyer j = i % 16;
3104 1.21.2.3 bouyer if (j == 0 && i != 0) {
3105 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3106 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), s1, s2));
3107 1.21.2.3 bouyer s2[0] = '\0';
3108 1.21.2.3 bouyer }
3109 1.21.2.3 bouyer sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3110 1.21.2.3 bouyer }
3111 1.21.2.3 bouyer if (buflen > printlen)
3112 1.21.2.3 bouyer sprintf(s3, " ...");
3113 1.21.2.3 bouyer DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3114 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), s1, s2, s3));
3115 1.21.2.3 bouyer }
3116 1.21.2.3 bouyer #endif
3117 1.21.2.3 bouyer
3118 1.21.2.3 bouyer
3119 1.21.2.3 bouyer
3120 1.21.2.3 bouyer
3121 1.21.2.3 bouyer
3122 1.21.2.3 bouyer
3123 1.21.2.3 bouyer
3124 1.21.2.3 bouyer
3125 1.21.2.3 bouyer #if defined(__NetBSD__) || defined(__OpenBSD__)
3126 1.21.2.3 bouyer Static void
3127 1.21.2.3 bouyer umass_scsipi_request(struct scsipi_channel *chan,
3128 1.21.2.3 bouyer scsipi_adapter_req_t req, void *arg)
3129 1.21.2.3 bouyer {
3130 1.21.2.3 bouyer struct scsipi_adapter *adapt = chan->chan_adapter;
3131 1.21.2.3 bouyer struct scsipi_periph *periph;
3132 1.21.2.3 bouyer struct scsipi_xfer *xs;
3133 1.21.2.3 bouyer struct umass_softc *sc = (void *)adapt->adapt_dev;
3134 1.21.2.3 bouyer struct scsipi_generic *cmd, trcmd;
3135 1.21.2.3 bouyer int cmdlen;
3136 1.21.2.3 bouyer int dir;
3137 1.21.2.3 bouyer #ifdef UMASS_DEBUG
3138 1.21.2.3 bouyer microtime(&sc->tv);
3139 1.21.2.3 bouyer #endif
3140 1.21.2.3 bouyer
3141 1.21.2.3 bouyer switch(req) {
3142 1.21.2.3 bouyer case ADAPTER_REQ_RUN_XFER:
3143 1.21.2.3 bouyer xs = arg;
3144 1.21.2.3 bouyer periph = xs->xs_periph;
3145 1.21.2.3 bouyer DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
3146 1.21.2.3 bouyer
3147 1.21.2.3 bouyer DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %lu.%06lu: %d:%d "
3148 1.21.2.3 bouyer "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
3149 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), sc->tv.tv_sec, sc->tv.tv_usec,
3150 1.21.2.3 bouyer periph->periph_target, periph->periph_lun,
3151 1.21.2.3 bouyer xs, xs->cmd->opcode, xs->datalen,
3152 1.21.2.3 bouyer periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
3153 1.21.2.3 bouyer #if defined(USB_DEBUG) && defined(SCSIDEBUG)
3154 1.21.2.3 bouyer if (umassdebug & UDMASS_SCSI)
3155 1.21.2.3 bouyer show_scsipi_xs(xs);
3156 1.21.2.3 bouyer else if (umassdebug & ~UDMASS_CMD)
3157 1.21.2.3 bouyer show_scsipi_cmd(xs);
3158 1.21.2.3 bouyer #endif
3159 1.21.2.3 bouyer
3160 1.21.2.3 bouyer if (sc->sc_dying) {
3161 1.21.2.3 bouyer xs->error = XS_DRIVER_STUFFUP;
3162 1.21.2.3 bouyer goto done;
3163 1.21.2.3 bouyer }
3164 1.21.2.3 bouyer
3165 1.21.2.3 bouyer #ifdef UMASS_DEBUG
3166 1.21.2.1 thorpej if (periph->periph_target != UMASS_SCSIID_DEVICE) {
3167 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
3168 1.21.2.1 thorpej USBDEVNAME(sc->sc_dev),
3169 1.21.2.1 thorpej periph->periph_target));
3170 1.21.2.1 thorpej xs->error = XS_DRIVER_STUFFUP;
3171 1.21.2.3 bouyer goto done;
3172 1.21.2.1 thorpej }
3173 1.21.2.1 thorpej #endif
3174 1.21.2.1 thorpej
3175 1.21.2.3 bouyer if (xs->cmd->opcode == SCSI_MODE_SENSE &&
3176 1.21.2.3 bouyer (periph->periph_quirks & PQUIRK_NOMODESENSE)) {
3177 1.21.2.3 bouyer /*printf("%s: SCSI_MODE_SENSE\n", USBDEVNAME(sc->sc_dev));*/
3178 1.21.2.3 bouyer xs->error = XS_TIMEOUT;
3179 1.21.2.3 bouyer goto done;
3180 1.21.2.3 bouyer }
3181 1.21.2.3 bouyer
3182 1.21.2.3 bouyer if (xs->cmd->opcode == START_STOP &&
3183 1.21.2.3 bouyer (sc->quirks & NO_START_STOP)) {
3184 1.21.2.3 bouyer /*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
3185 1.21.2.3 bouyer xs->error = XS_NOERROR;
3186 1.21.2.3 bouyer goto done;
3187 1.21.2.3 bouyer }
3188 1.21.2.3 bouyer
3189 1.21.2.1 thorpej dir = DIR_NONE;
3190 1.21.2.1 thorpej if (xs->datalen) {
3191 1.21.2.1 thorpej switch (xs->xs_control &
3192 1.21.2.3 bouyer (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
3193 1.21.2.1 thorpej case XS_CTL_DATA_IN:
3194 1.21.2.1 thorpej dir = DIR_IN;
3195 1.21.2.1 thorpej break;
3196 1.21.2.1 thorpej case XS_CTL_DATA_OUT:
3197 1.21.2.1 thorpej dir = DIR_OUT;
3198 1.21.2.1 thorpej break;
3199 1.21.2.1 thorpej }
3200 1.21.2.1 thorpej }
3201 1.18 augustss
3202 1.21.2.3 bouyer if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
3203 1.21.2.3 bouyer printf("umass_cmd: large datalen, %d\n", xs->datalen);
3204 1.21.2.3 bouyer xs->error = XS_DRIVER_STUFFUP;
3205 1.21.2.3 bouyer goto done;
3206 1.21.2.3 bouyer }
3207 1.21.2.3 bouyer
3208 1.21.2.3 bouyer cmd = xs->cmd;
3209 1.21.2.3 bouyer cmdlen = xs->cmdlen;
3210 1.21.2.3 bouyer if (sc->proto & PROTO_UFI) {
3211 1.21.2.3 bouyer if (!umass_ufi_transform(sc, cmd, cmdlen, &trcmd,
3212 1.21.2.3 bouyer &cmdlen)) {
3213 1.21.2.3 bouyer xs->error = XS_DRIVER_STUFFUP;
3214 1.21.2.3 bouyer goto done;
3215 1.21.2.3 bouyer }
3216 1.21.2.3 bouyer cmd = &trcmd;
3217 1.1 thorpej
3218 1.21.2.3 bouyer }
3219 1.15 thorpej
3220 1.21.2.3 bouyer if (xs->xs_control & XS_CTL_POLL) {
3221 1.21.2.3 bouyer /* Use sync transfer. XXX Broken! */
3222 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI,
3223 1.21.2.3 bouyer ("umass_scsi_cmd: sync dir=%d\n", dir));
3224 1.21.2.3 bouyer sc->sc_xfer_flags = USBD_SYNCHRONOUS;
3225 1.21.2.3 bouyer sc->sc_sync_status = USBD_INVAL;
3226 1.21.2.3 bouyer sc->transfer(sc, periph->periph_lun, cmd, cmdlen,
3227 1.21.2.3 bouyer xs->data, xs->datalen, dir, 0, xs);
3228 1.21.2.3 bouyer sc->sc_xfer_flags = 0;
3229 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
3230 1.21.2.3 bouyer sc->sc_sync_status));
3231 1.21.2.3 bouyer switch (sc->sc_sync_status) {
3232 1.21.2.3 bouyer case USBD_NORMAL_COMPLETION:
3233 1.21.2.3 bouyer xs->error = XS_NOERROR;
3234 1.21.2.3 bouyer break;
3235 1.21.2.3 bouyer case USBD_TIMEOUT:
3236 1.21.2.3 bouyer xs->error = XS_TIMEOUT;
3237 1.21.2.3 bouyer break;
3238 1.21.2.3 bouyer default:
3239 1.21.2.3 bouyer xs->error = XS_DRIVER_STUFFUP;
3240 1.21.2.3 bouyer break;
3241 1.21.2.3 bouyer }
3242 1.21.2.3 bouyer goto done;
3243 1.21.2.1 thorpej } else {
3244 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
3245 1.21.2.3 bouyer " datalen=%d\n",
3246 1.21.2.3 bouyer dir, cmdlen, xs->datalen));
3247 1.21.2.3 bouyer sc->transfer(sc, periph->periph_lun, cmd, cmdlen,
3248 1.21.2.3 bouyer xs->data, xs->datalen, dir, umass_scsipi_cb, xs);
3249 1.21.2.3 bouyer return;
3250 1.21.2.3 bouyer }
3251 1.21.2.3 bouyer
3252 1.21.2.3 bouyer /* Return if command finishes early. */
3253 1.21.2.3 bouyer done:
3254 1.21.2.3 bouyer xs->xs_status |= XS_STS_DONE;
3255 1.21.2.3 bouyer scsipi_done(xs);
3256 1.21.2.3 bouyer return;
3257 1.21.2.3 bouyer default:
3258 1.21.2.3 bouyer /* Not supported, nothing to do. */
3259 1.21.2.3 bouyer }
3260 1.21.2.3 bouyer }
3261 1.21.2.3 bouyer
3262 1.21.2.3 bouyer Static void
3263 1.21.2.3 bouyer umass_scsipi_minphys(struct buf *bp)
3264 1.21.2.3 bouyer {
3265 1.21.2.5 bouyer #ifdef DIAGNOSTIC
3266 1.21.2.5 bouyer if (bp->b_bcount <= 0) {
3267 1.21.2.5 bouyer printf("umass_scsipi_minphys count(%ld) <= 0\n",
3268 1.21.2.5 bouyer bp->b_bcount);
3269 1.21.2.5 bouyer bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
3270 1.21.2.5 bouyer }
3271 1.21.2.5 bouyer #endif
3272 1.21.2.3 bouyer if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
3273 1.21.2.3 bouyer bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
3274 1.21.2.3 bouyer minphys(bp);
3275 1.21.2.3 bouyer }
3276 1.21.2.3 bouyer
3277 1.21.2.3 bouyer int
3278 1.21.2.3 bouyer umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t arg,
3279 1.21.2.3 bouyer int flag, struct proc *p)
3280 1.21.2.3 bouyer {
3281 1.21.2.3 bouyer /*struct umass_softc *sc = link->adapter_softc;*/
3282 1.21.2.3 bouyer
3283 1.21.2.3 bouyer switch (cmd) {
3284 1.21.2.3 bouyer #if 0
3285 1.21.2.3 bouyer case SCBUSIORESET:
3286 1.21.2.3 bouyer ccb->ccb_h.status = CAM_REQ_INPROG;
3287 1.21.2.3 bouyer umass_reset(sc, umass_cam_cb, (void *) ccb);
3288 1.21.2.3 bouyer return (0);
3289 1.21.2.3 bouyer #endif
3290 1.21.2.3 bouyer default:
3291 1.21.2.3 bouyer return (ENOTTY);
3292 1.21.2.3 bouyer }
3293 1.21.2.3 bouyer }
3294 1.21.2.3 bouyer
3295 1.21.2.3 bouyer Static int
3296 1.21.2.3 bouyer umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
3297 1.21.2.3 bouyer u_long sectors)
3298 1.21.2.3 bouyer {
3299 1.21.2.3 bouyer struct umass_softc *sc =
3300 1.21.2.3 bouyer (void *)periph->periph_channel->chan_adapter->adapt_dev;
3301 1.21.2.3 bouyer
3302 1.21.2.3 bouyer /* If it's not a floppy, we don't know what to do. */
3303 1.21.2.3 bouyer if (!(sc->proto & PROTO_UFI))
3304 1.21.2.3 bouyer return (0);
3305 1.21.2.3 bouyer
3306 1.21.2.3 bouyer switch (sectors) {
3307 1.21.2.3 bouyer case 1440:
3308 1.21.2.3 bouyer /* Most likely a single density 3.5" floppy. */
3309 1.21.2.3 bouyer dp->heads = 2;
3310 1.21.2.3 bouyer dp->sectors = 9;
3311 1.21.2.3 bouyer dp->cyls = 80;
3312 1.21.2.3 bouyer return (1);
3313 1.21.2.3 bouyer case 2880:
3314 1.21.2.3 bouyer /* Most likely a double density 3.5" floppy. */
3315 1.21.2.3 bouyer dp->heads = 2;
3316 1.21.2.3 bouyer dp->sectors = 18;
3317 1.21.2.3 bouyer dp->cyls = 80;
3318 1.21.2.3 bouyer return (1);
3319 1.21.2.3 bouyer default:
3320 1.21.2.3 bouyer return (0);
3321 1.21.2.3 bouyer }
3322 1.21.2.3 bouyer }
3323 1.21.2.3 bouyer
3324 1.21.2.3 bouyer Static void
3325 1.21.2.3 bouyer umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
3326 1.21.2.3 bouyer {
3327 1.21.2.3 bouyer struct scsipi_xfer *xs = priv;
3328 1.21.2.3 bouyer struct scsipi_periph *periph = xs->xs_periph;
3329 1.21.2.3 bouyer int cmdlen;
3330 1.21.2.3 bouyer int s;
3331 1.21.2.3 bouyer #ifdef UMASS_DEBUG
3332 1.21.2.3 bouyer struct timeval tv;
3333 1.21.2.3 bouyer u_int delta;
3334 1.21.2.3 bouyer microtime(&tv);
3335 1.21.2.3 bouyer delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
3336 1.21.2.3 bouyer #endif
3337 1.21.2.3 bouyer
3338 1.21.2.3 bouyer DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu, delta=%u: xs=%p residue=%d"
3339 1.21.2.3 bouyer " status=%d\n", tv.tv_sec, tv.tv_usec, delta, xs, residue, status));
3340 1.21.2.3 bouyer
3341 1.21.2.3 bouyer xs->resid = residue;
3342 1.21.2.3 bouyer
3343 1.21.2.3 bouyer switch (status) {
3344 1.21.2.3 bouyer case STATUS_CMD_OK:
3345 1.21.2.3 bouyer xs->error = XS_NOERROR;
3346 1.21.2.3 bouyer break;
3347 1.21.2.3 bouyer
3348 1.21.2.3 bouyer case STATUS_CMD_UNKNOWN:
3349 1.21.2.3 bouyer case STATUS_CMD_FAILED:
3350 1.21.2.3 bouyer /* fetch sense data */
3351 1.21.2.3 bouyer memset(&sc->sc_sense_cmd, 0, sizeof(sc->sc_sense_cmd));
3352 1.21.2.3 bouyer sc->sc_sense_cmd.opcode = REQUEST_SENSE;
3353 1.21.2.3 bouyer sc->sc_sense_cmd.byte2 = periph->periph_lun <<
3354 1.21.2.3 bouyer SCSI_CMD_LUN_SHIFT;
3355 1.21.2.3 bouyer sc->sc_sense_cmd.length = sizeof(xs->sense);
3356 1.21.2.3 bouyer
3357 1.21.2.3 bouyer cmdlen = sizeof(sc->sc_sense_cmd);
3358 1.21.2.3 bouyer if (sc->proto & PROTO_UFI) /* XXX */
3359 1.21.2.3 bouyer cmdlen = UFI_COMMAND_LENGTH;
3360 1.21.2.3 bouyer sc->transfer(sc, periph->periph_lun,
3361 1.21.2.3 bouyer &sc->sc_sense_cmd, cmdlen,
3362 1.21.2.3 bouyer &xs->sense, sizeof(xs->sense), DIR_IN,
3363 1.21.2.3 bouyer umass_scsipi_sense_cb, xs);
3364 1.21.2.3 bouyer return;
3365 1.21.2.3 bouyer
3366 1.21.2.3 bouyer case STATUS_WIRE_FAILED:
3367 1.21.2.3 bouyer xs->error = XS_RESET;
3368 1.21.2.3 bouyer break;
3369 1.21.2.3 bouyer
3370 1.21.2.3 bouyer default:
3371 1.21.2.3 bouyer panic("%s: Unknown status %d in umass_scsipi_cb\n",
3372 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), status);
3373 1.21.2.3 bouyer }
3374 1.21.2.3 bouyer
3375 1.21.2.3 bouyer xs->xs_status |= XS_STS_DONE;
3376 1.21.2.3 bouyer
3377 1.21.2.3 bouyer DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu: return xs->error="
3378 1.21.2.3 bouyer "%d, xs->xs_status=0x%x xs->resid=%d\n",
3379 1.21.2.3 bouyer tv.tv_sec, tv.tv_usec,
3380 1.21.2.3 bouyer xs->error, xs->xs_status, xs->resid));
3381 1.21.2.1 thorpej
3382 1.21.2.3 bouyer s = splbio();
3383 1.21.2.3 bouyer scsipi_done(xs);
3384 1.21.2.3 bouyer splx(s);
3385 1.21.2.3 bouyer }
3386 1.21.2.3 bouyer
3387 1.21.2.3 bouyer /*
3388 1.21.2.3 bouyer * Finalise a completed autosense operation
3389 1.21.2.3 bouyer */
3390 1.21.2.3 bouyer Static void
3391 1.21.2.3 bouyer umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
3392 1.21.2.3 bouyer int status)
3393 1.21.2.3 bouyer {
3394 1.21.2.3 bouyer struct scsipi_xfer *xs = priv;
3395 1.21.2.3 bouyer int s;
3396 1.21.2.3 bouyer
3397 1.21.2.3 bouyer DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
3398 1.21.2.3 bouyer "status=%d\n", xs, residue, status));
3399 1.21.2.3 bouyer
3400 1.21.2.3 bouyer switch (status) {
3401 1.21.2.3 bouyer case STATUS_CMD_OK:
3402 1.21.2.3 bouyer case STATUS_CMD_UNKNOWN:
3403 1.21.2.3 bouyer /* getting sense data succeeded */
3404 1.21.2.3 bouyer if (xs->cmd->opcode == INQUIRY && (xs->resid < xs->datalen
3405 1.21.2.3 bouyer || ((sc->quirks & RS_NO_CLEAR_UA) /* XXX */) )) {
3406 1.21.2.1 thorpej /*
3407 1.21.2.3 bouyer * Some drivers return SENSE errors even after INQUIRY.
3408 1.21.2.3 bouyer * The upper layer doesn't like that.
3409 1.21.2.1 thorpej */
3410 1.21.2.3 bouyer xs->error = XS_NOERROR;
3411 1.21.2.3 bouyer break;
3412 1.21.2.3 bouyer }
3413 1.21.2.3 bouyer /* XXX look at residue */
3414 1.21.2.3 bouyer if (residue == 0 || residue == 14)/* XXX */
3415 1.21.2.3 bouyer xs->error = XS_SENSE;
3416 1.21.2.3 bouyer else
3417 1.21.2.3 bouyer xs->error = XS_SHORTSENSE;
3418 1.21.2.3 bouyer break;
3419 1.21.2.3 bouyer default:
3420 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3421 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), status));
3422 1.21.2.3 bouyer xs->error = XS_DRIVER_STUFFUP;
3423 1.21.2.3 bouyer break;
3424 1.21.2.3 bouyer }
3425 1.21.2.1 thorpej
3426 1.21.2.3 bouyer xs->xs_status |= XS_STS_DONE;
3427 1.21.2.1 thorpej
3428 1.21.2.3 bouyer DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
3429 1.21.2.3 bouyer "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
3430 1.21.2.3 bouyer xs->resid));
3431 1.1 thorpej
3432 1.21.2.3 bouyer s = splbio();
3433 1.21.2.3 bouyer scsipi_done(xs);
3434 1.21.2.3 bouyer splx(s);
3435 1.21.2.3 bouyer }
3436 1.21.2.3 bouyer
3437 1.21.2.3 bouyer /*
3438 1.21.2.3 bouyer * UFI specific functions
3439 1.21.2.3 bouyer */
3440 1.21.2.3 bouyer
3441 1.21.2.3 bouyer Static int
3442 1.21.2.3 bouyer umass_ufi_transform(struct umass_softc *sc, struct scsipi_generic *cmd,
3443 1.21.2.3 bouyer int cmdlen, struct scsipi_generic *rcmd, int *rcmdlen)
3444 1.21.2.3 bouyer {
3445 1.21.2.3 bouyer *rcmdlen = UFI_COMMAND_LENGTH;
3446 1.21.2.3 bouyer memset(rcmd, 0, sizeof *rcmd);
3447 1.21.2.1 thorpej
3448 1.21.2.3 bouyer /* Handle any quirks */
3449 1.21.2.3 bouyer if (cmd->opcode == TEST_UNIT_READY
3450 1.21.2.3 bouyer && (sc->quirks & NO_TEST_UNIT_READY)) {
3451 1.21.2.1 thorpej /*
3452 1.21.2.3 bouyer * Some devices do not support this command.
3453 1.21.2.3 bouyer * Start Stop Unit should give the same results
3454 1.21.2.1 thorpej */
3455 1.21.2.3 bouyer DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3456 1.21.2.3 bouyer "to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
3457 1.21.2.3 bouyer cmd->opcode = START_STOP;
3458 1.21.2.3 bouyer cmd->bytes[3] = SSS_START;
3459 1.21.2.3 bouyer return 1;
3460 1.21.2.3 bouyer }
3461 1.21.2.3 bouyer
3462 1.21.2.3 bouyer switch (cmd->opcode) {
3463 1.21.2.3 bouyer /* Commands of which the format has been verified. They should work. */
3464 1.21.2.3 bouyer case TEST_UNIT_READY:
3465 1.21.2.3 bouyer case SCSI_REZERO_UNIT:
3466 1.21.2.3 bouyer case REQUEST_SENSE:
3467 1.21.2.3 bouyer case INQUIRY:
3468 1.21.2.3 bouyer case START_STOP:
3469 1.21.2.3 bouyer /*case SEND_DIAGNOSTIC: ??*/
3470 1.21.2.3 bouyer case PREVENT_ALLOW:
3471 1.21.2.3 bouyer case READ_CAPACITY:
3472 1.21.2.3 bouyer case READ_BIG:
3473 1.21.2.3 bouyer case WRITE_BIG:
3474 1.21.2.3 bouyer case POSITION_TO_ELEMENT: /* SEEK_10 */
3475 1.21.2.3 bouyer case SCSI_MODE_SELECT_BIG:
3476 1.21.2.3 bouyer case SCSI_MODE_SENSE_BIG:
3477 1.21.2.3 bouyer /* Copy the command into the (zeroed out) destination buffer */
3478 1.21.2.3 bouyer memcpy(rcmd, cmd, cmdlen);
3479 1.21.2.3 bouyer return (1); /* success */
3480 1.21.2.3 bouyer
3481 1.21.2.3 bouyer /*
3482 1.21.2.3 bouyer * Other UFI commands: FORMAT_UNIT, MODE_SELECT, READ_FORMAT_CAPACITY,
3483 1.21.2.3 bouyer * VERIFY, WRITE_AND_VERIFY.
3484 1.21.2.3 bouyer * These should be checked whether they somehow can be made to fit.
3485 1.21.2.3 bouyer */
3486 1.21.2.3 bouyer
3487 1.21.2.3 bouyer /* These commands are known _not_ to work. They should be converted. */
3488 1.21.2.3 bouyer case SCSI_READ_COMMAND:
3489 1.21.2.3 bouyer case SCSI_WRITE_COMMAND:
3490 1.21.2.3 bouyer case SCSI_MODE_SENSE:
3491 1.21.2.3 bouyer case SCSI_MODE_SELECT:
3492 1.21.2.3 bouyer default:
3493 1.21.2.3 bouyer printf("%s: Unsupported UFI command 0x%02x",
3494 1.21.2.3 bouyer USBDEVNAME(sc->sc_dev), cmd->opcode);
3495 1.21.2.3 bouyer if (cmdlen == 6)
3496 1.21.2.3 bouyer printf(", 6 byte command should have been converted");
3497 1.21.2.3 bouyer printf("\n");
3498 1.21.2.3 bouyer return (0); /* failure */
3499 1.21.2.1 thorpej }
3500 1.1 thorpej }
3501 1.1 thorpej
3502 1.21.2.3 bouyer #if NATAPIBUS > 0
3503 1.21.2.3 bouyer Static void
3504 1.21.2.3 bouyer umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
3505 1.1 thorpej {
3506 1.21.2.3 bouyer struct scsipi_channel *chan = atapi->sc_channel;
3507 1.21.2.3 bouyer struct scsipi_periph *periph;
3508 1.21.2.3 bouyer struct scsipibus_attach_args sa;
3509 1.21.2.3 bouyer struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
3510 1.21.2.3 bouyer char vendor[33], product[65], revision[17];
3511 1.21.2.3 bouyer struct scsipi_inquiry_data inqbuf;
3512 1.1 thorpej
3513 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
3514 1.21.2.3 bouyer atapi, target));
3515 1.21.2.3 bouyer
3516 1.21.2.3 bouyer if (target != 0) /* only probe drive 0 */
3517 1.21.2.3 bouyer return;
3518 1.21.2.3 bouyer
3519 1.21.2.3 bouyer /* skip if already attached */
3520 1.21.2.3 bouyer if (scsipi_lookup_periph(chan, target, 0) != NULL)
3521 1.21.2.3 bouyer return;
3522 1.21.2.3 bouyer
3523 1.21.2.3 bouyer periph = scsipi_alloc_periph(M_NOWAIT);
3524 1.21.2.3 bouyer if (periph == NULL) {
3525 1.21.2.3 bouyer printf("%s: can't allocate link for drive %d\n",
3526 1.21.2.3 bouyer atapi->sc_dev.dv_xname, target);
3527 1.21.2.3 bouyer return;
3528 1.21.2.3 bouyer }
3529 1.21.2.3 bouyer
3530 1.21.2.3 bouyer DIF(UDMASS_UPPER, periph->periph_dbflags |= DEBUGLEVEL);
3531 1.21.2.3 bouyer periph->periph_channel = chan;
3532 1.21.2.3 bouyer periph->periph_switch = &atapi_probe_periphsw;
3533 1.21.2.3 bouyer periph->periph_target = target;
3534 1.21.2.3 bouyer
3535 1.21.2.3 bouyer DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
3536 1.21.2.3 bouyer /* Now go ask the device all about itself. */
3537 1.21.2.3 bouyer memset(&inqbuf, 0, sizeof(inqbuf));
3538 1.21.2.3 bouyer if (scsipi_inquire(periph, &inqbuf,
3539 1.21.2.3 bouyer XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0)
3540 1.21.2.3 bouyer goto bad;
3541 1.21.2.3 bouyer
3542 1.21.2.3 bouyer scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
3543 1.21.2.3 bouyer scsipi_strvis(product, 65, inqbuf.product, 16);
3544 1.21.2.3 bouyer scsipi_strvis(revision, 17, inqbuf.revision, 4);
3545 1.21.2.3 bouyer
3546 1.21.2.3 bouyer sa.sa_periph = periph;
3547 1.21.2.3 bouyer sa.sa_inqbuf.type = inqbuf.device;
3548 1.21.2.3 bouyer sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
3549 1.21.2.3 bouyer T_REMOV : T_FIXED;
3550 1.21.2.3 bouyer if (sa.sa_inqbuf.removable)
3551 1.21.2.3 bouyer periph->periph_flags |= PERIPH_REMOVABLE;
3552 1.21.2.3 bouyer /* XXX how? sc_link->scsipi_atapi.cap |= ACAP_LEN;*/
3553 1.21.2.3 bouyer sa.sa_inqbuf.vendor = vendor;
3554 1.21.2.3 bouyer sa.sa_inqbuf.product = product;
3555 1.21.2.3 bouyer sa.sa_inqbuf.revision = revision;
3556 1.21.2.3 bouyer sa.sa_inqptr = NULL;
3557 1.21.2.3 bouyer
3558 1.21.2.3 bouyer drvp->drv_softc = atapi_probe_device(atapi, target, periph, &sa);
3559 1.21.2.3 bouyer /* atapi_probe_device() frees the periph when there is no device. */
3560 1.21.2.3 bouyer return;
3561 1.21.2.3 bouyer
3562 1.21.2.3 bouyer bad:
3563 1.21.2.3 bouyer free(periph, M_DEVBUF);
3564 1.21.2.3 bouyer return;
3565 1.1 thorpej }
3566 1.21.2.3 bouyer #endif
3567 1.21.2.3 bouyer #endif
3568