When attached to a high-speed device, report a more appropriate
[dragonfly/netmp.git] / sys / dev / usbmisc / umass / umass.c
blob8bc241df1153b99c2fc30b1465c92fb687121134
1 /*-
2 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
3 * Nick Hibma <n_hibma@freebsd.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
28 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.96 2003/12/19 12:19:11 sanpei Exp $
29 * $DragonFly: src/sys/dev/usbmisc/umass/umass.c,v 1.33 2008/01/02 10:43:28 hasso Exp $
33 * Universal Serial Bus Mass Storage Class specs:
34 * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
35 * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
36 * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
37 * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
41 * Ported to NetBSD by Lennart Augustsson <augustss@netbsd.org>.
42 * Parts of the code written my Jason R. Thorpe <thorpej@shagadelic.org>.
46 * The driver handles 3 Wire Protocols
47 * - Command/Bulk/Interrupt (CBI)
48 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
49 * - Mass Storage Bulk-Only (BBB)
50 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
52 * Over these wire protocols it handles the following command protocols
53 * - SCSI
54 * - UFI (floppy command set)
55 * - 8070i (ATAPI)
57 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
58 * sc->transform method is used to convert the commands into the appropriate
59 * format (if at all necessary). For example, UFI requires all commands to be
60 * 12 bytes in length amongst other things.
62 * The source code below is marked and can be split into a number of pieces
63 * (in this order):
65 * - probe/attach/detach
66 * - generic transfer routines
67 * - BBB
68 * - CBI
69 * - CBI_I (in addition to functions from CBI)
70 * - CAM (Common Access Method)
71 * - SCSI
72 * - UFI
73 * - 8070i (ATAPI)
75 * The protocols are implemented using a state machine, for the transfers as
76 * well as for the resets. The state machine is contained in umass_*_state.
77 * The state machine is started through either umass_*_transfer or
78 * umass_*_reset.
80 * The reason for doing this is a) CAM performs a lot better this way and b) it
81 * avoids using tsleep from interrupt context (for example after a failed
82 * transfer).
86 * The SCSI related part of this driver has been derived from the
87 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
89 * The CAM layer uses so called actions which are messages sent to the host
90 * adapter for completion. The actions come in through umass_cam_action. The
91 * appropriate block of routines is called depending on the transport protocol
92 * in use. When the transfer has finished, these routines call
93 * umass_cam_cb again to complete the CAM command.
97 * XXX Currently CBI with CCI is not supported because it bombs the system
98 * when the device is detached (low frequency interrupts are detached
99 * too late.
101 #undef CBI_I
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/module.h>
107 #include <sys/bus.h>
108 #include <sys/sysctl.h>
110 #include <bus/usb/usb.h>
111 #include <bus/usb/usbdi.h>
112 #include <bus/usb/usbdi_util.h>
114 #include <bus/cam/cam.h>
115 #include <bus/cam/cam_ccb.h>
116 #include <bus/cam/cam_sim.h>
117 #include <bus/cam/cam_xpt_sim.h>
118 #include <bus/cam/scsi/scsi_all.h>
119 #include <bus/cam/scsi/scsi_da.h>
120 #include <bus/cam/scsi/scsi_cd.h>
121 #include <bus/cam/scsi/scsi_ch.h>
122 #include <dev/disk/ata/atapi-all.h>
124 #include <bus/cam/cam_periph.h>
126 #ifdef USB_DEBUG
127 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
128 #define DPRINTF(m, x) if (umassdebug & (m)) kprintf x
129 #define UDMASS_GEN 0x00010000 /* general */
130 #define UDMASS_SCSI 0x00020000 /* scsi */
131 #define UDMASS_UFI 0x00040000 /* ufi command set */
132 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
133 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
134 #define UDMASS_USB 0x00100000 /* USB general */
135 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
136 #define UDMASS_CBI 0x00400000 /* CBI transfers */
137 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
138 #define UDMASS_ALL 0xffff0000 /* all of the above */
139 int umassdebug = 0;
140 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
141 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
142 &umassdebug, 0, "umass debug level");
143 #else
144 #define DIF(m, x) /* nop */
145 #define DPRINTF(m, x) /* nop */
146 #endif
149 /* Generic definitions */
151 /* Direction for umass_*_transfer */
152 #define DIR_NONE 0
153 #define DIR_IN 1
154 #define DIR_OUT 2
156 /* device name */
157 #define DEVNAME "umass"
158 #define DEVNAME_SIM "umass-sim"
160 #define UMASS_MAX_TRANSFER_SIZE 65536
161 /* Approximate maximum transfer speeds (assumes 33% overhead). */
162 #define UMASS_FULL_TRANSFER_SPEED 1000
163 #define UMASS_HIGH_TRANSFER_SPEED 40000
164 #define UMASS_FLOPPY_TRANSFER_SPEED 20
166 #define UMASS_TIMEOUT 5000 /* msecs */
168 /* CAM specific definitions */
170 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
171 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
173 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
176 /* Bulk-Only features */
178 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
179 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
181 /* Command Block Wrapper */
182 typedef struct {
183 uDWord dCBWSignature;
184 # define CBWSIGNATURE 0x43425355
185 uDWord dCBWTag;
186 uDWord dCBWDataTransferLength;
187 uByte bCBWFlags;
188 # define CBWFLAGS_OUT 0x00
189 # define CBWFLAGS_IN 0x80
190 uByte bCBWLUN;
191 uByte bCDBLength;
192 # define CBWCDBLENGTH 16
193 uByte CBWCDB[CBWCDBLENGTH];
194 } umass_bbb_cbw_t;
195 #define UMASS_BBB_CBW_SIZE 31
197 /* Command Status Wrapper */
198 typedef struct {
199 uDWord dCSWSignature;
200 # define CSWSIGNATURE 0x53425355
201 # define CSWSIGNATURE_OLYMPUS_C1 0x55425355
202 uDWord dCSWTag;
203 uDWord dCSWDataResidue;
204 uByte bCSWStatus;
205 # define CSWSTATUS_GOOD 0x0
206 # define CSWSTATUS_FAILED 0x1
207 # define CSWSTATUS_PHASE 0x2
208 } umass_bbb_csw_t;
209 #define UMASS_BBB_CSW_SIZE 13
211 /* CBI features */
213 #define UR_CBI_ADSC 0x00
215 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
217 typedef union {
218 struct {
219 unsigned char type;
220 #define IDB_TYPE_CCI 0x00
221 unsigned char value;
222 #define IDB_VALUE_PASS 0x00
223 #define IDB_VALUE_FAIL 0x01
224 #define IDB_VALUE_PHASE 0x02
225 #define IDB_VALUE_PERSISTENT 0x03
226 #define IDB_VALUE_STATUS_MASK 0x03
227 } common;
229 struct {
230 unsigned char asc;
231 unsigned char ascq;
232 } ufi;
233 } umass_cbi_sbl_t;
237 struct umass_softc; /* see below */
239 typedef void (*transfer_cb_f) (struct umass_softc *sc, void *priv,
240 int residue, int status);
241 #define STATUS_CMD_OK 0 /* everything ok */
242 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
243 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
244 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
246 typedef void (*wire_reset_f) (struct umass_softc *sc, int status);
247 typedef void (*wire_transfer_f) (struct umass_softc *sc, int lun,
248 void *cmd, int cmdlen, void *data, int datalen,
249 int dir, transfer_cb_f cb, void *priv);
250 typedef void (*wire_state_f) (usbd_xfer_handle xfer,
251 usbd_private_handle priv, usbd_status err);
253 typedef int (*command_transform_f) (struct umass_softc *sc,
254 unsigned char *cmd, int cmdlen,
255 unsigned char **rcmd, int *rcmdlen);
258 struct umass_devdescr_t {
259 u_int32_t vendor;
260 u_int32_t product;
261 u_int32_t release;
262 # define WILDCARD_ID 0xffffffff
263 # define EOT_ID 0xfffffffe
265 /* wire and command protocol */
266 u_int16_t proto;
267 # define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
268 # define UMASS_PROTO_CBI 0x0002
269 # define UMASS_PROTO_CBI_I 0x0004
270 # define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
271 # define UMASS_PROTO_SCSI 0x0100 /* command protocol */
272 # define UMASS_PROTO_ATAPI 0x0200
273 # define UMASS_PROTO_UFI 0x0400
274 # define UMASS_PROTO_RBC 0x0800
275 # define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
277 /* Device specific quirks */
278 u_int16_t quirks;
279 # define NO_QUIRKS 0x0000
280 /* The drive does not support Test Unit Ready. Convert to Start Unit
282 # define NO_TEST_UNIT_READY 0x0001
283 /* The drive does not reset the Unit Attention state after REQUEST
284 * SENSE has been sent. The INQUIRY command does not reset the UA
285 * either, and so CAM runs in circles trying to retrieve the initial
286 * INQUIRY data.
288 # define RS_NO_CLEAR_UA 0x0002
289 /* The drive does not support START STOP. */
290 # define NO_START_STOP 0x0004
291 /* Don't ask for full inquiry data (255b). */
292 # define FORCE_SHORT_INQUIRY 0x0008
293 /* Needs to be initialised the Shuttle way */
294 # define SHUTTLE_INIT 0x0010
295 /* Drive needs to be switched to alternate iface 1 */
296 # define ALT_IFACE_1 0x0020
297 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
298 # define FLOPPY_SPEED 0x0040
299 /* The device can't count and gets the residue of transfers wrong */
300 # define IGNORE_RESIDUE 0x0080
301 /* No GetMaxLun call */
302 # define NO_GETMAXLUN 0x0100
303 /* The device uses a weird CSWSIGNATURE. */
304 # define WRONG_CSWSIG 0x0200
305 /* Device cannot handle INQUIRY so fake a generic response */
306 # define NO_INQUIRY 0x0400
307 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
308 # define NO_INQUIRY_EVPD 0x0800
311 static struct umass_devdescr_t umass_devdescrs[] = {
312 /* All Asahi Optical products */
313 { .vendor = 0x0a17, .product = WILDCARD_ID, .release = WILDCARD_ID,
314 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
315 .quirks = RS_NO_CLEAR_UA
317 /* Fujiphoto mass storage products */
318 { .vendor = 0x04cb, .product = 0x0100, .release = WILDCARD_ID,
319 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
320 .quirks = RS_NO_CLEAR_UA
322 /* Genesys Logic GL641USB USB-IDE Bridge */
323 { .vendor = 0x05e3, .product = 0x0702, .release = WILDCARD_ID,
324 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
325 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
327 /* Genesys Logic GL641USB CompactFlash Card Reader */
328 { .vendor = 0x05e3, .product = 0x0700, .release = WILDCARD_ID,
329 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
330 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
332 /* Hitachi DVDCAM USB HS Interface */
333 { .vendor = 0x04a4, .product = 0x001e, .release = WILDCARD_ID,
334 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
335 .quirks = NO_INQUIRY
337 /* HP CD-Writer Plus 8200e */
338 { .vendor = 0x03f0, .product = 0x0207, .release = WILDCARD_ID,
339 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
340 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
342 /* In-System USB cable */
343 { .vendor = 0x05ab, .product = 0x081a, .release = WILDCARD_ID,
344 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
345 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
347 /* Iomega Zip 100 */
348 { .vendor = 0x059b, .product = 0x0001, .release = WILDCARD_ID,
349 /* XXX This is not correct as there are Zip drives that use ATAPI. */
350 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
351 .quirks = NO_TEST_UNIT_READY
353 /* Logitech DVD Multi-plus unit LDR-H443U2 */
354 { .vendor = 0x0789, .product = 0x00b3, .release = WILDCARD_ID,
355 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
356 .quirks = NO_QUIRKS
358 /* Melco USB-IDE Bridge: DUB-PxxG */
359 { .vendor = 0x0411, .product = 0x001c, .release = WILDCARD_ID,
360 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
361 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
363 /* Microtech USB CameraMate */
364 { .vendor = 0x07af, .product = 0x0006, .release = WILDCARD_ID,
365 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
366 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
368 /* M-Systems DiskOnKey */
369 { .vendor = 0x08ec, .product = 0x0010, .release = WILDCARD_ID,
370 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
371 .quirks = IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
373 /* M-Systems DiskOnKey */
374 { .vendor = 0x08ec, .product = 0x0011, .release = WILDCARD_ID,
375 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
376 .quirks = NO_QUIRKS
378 /* Olympus C-1 Digital Camera */
379 { .vendor = 0x07b4, .product = 0x0102, .release = WILDCARD_ID,
380 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
381 .quirks = WRONG_CSWSIG
383 /* Panasonic CD-R Drive KXL-CB20AN */
384 { .vendor = 0x04da, .product = 0x0d0a, .release = WILDCARD_ID,
385 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
386 .quirks = NO_QUIRKS
388 /* Panasonic DVD-ROM & CD-R/RW */
389 { .vendor = 0x04da, .product = 0x0d0e, .release = WILDCARD_ID,
390 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
391 .quirks = NO_QUIRKS
393 /* Pen USB 2.0 Flash Drive */
394 { .vendor = 0x0d7d, .product = 0x1300, .release = WILDCARD_ID,
395 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
396 .quirks = IGNORE_RESIDUE
398 /* ScanLogic SL11R-IDE */
399 { .vendor = 0x04ce, .product = 0x0002, .release = WILDCARD_ID,
400 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
401 .quirks = NO_QUIRKS
403 /* Shuttle Technology E-USB Bridge */
404 { .vendor = 0x04e6, .product = 0x0001, .release = WILDCARD_ID,
405 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
406 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
408 /* Sigmatel i-Bead 100 MP3 Player */
409 { .vendor = 0x066f, .product = 0x8008, .release = WILDCARD_ID,
410 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
411 .quirks = SHUTTLE_INIT
413 /* Sony DSC cameras */
414 { .vendor = 0x054c, .product = 0x0010, .release = WILDCARD_ID,
415 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
416 .quirks = NO_QUIRKS
418 /* Sony MSC memory stick slot */
419 { .vendor = 0x054c, .product = 0x0032, .release = WILDCARD_ID,
420 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
421 .quirks = NO_QUIRKS
423 /* Trek Technology ThumbDrive 8MB */
424 { .vendor = 0x0a16, .product = 0x9988, .release = WILDCARD_ID,
425 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
426 .quirks = IGNORE_RESIDUE
428 /* Yano U640MO-03 */
429 { .vendor = 0x094f, .product = 0x0101, .release = WILDCARD_ID,
430 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
431 .quirks = FORCE_SHORT_INQUIRY
433 { .vendor = EOT_ID, .product = EOT_ID, .release = EOT_ID,
434 .proto = 0, .quirks = 0 }
438 /* the per device structure */
439 struct umass_softc {
440 device_t sc_dev; /* base device */
441 usbd_device_handle sc_udev; /* USB device */
443 struct cam_sim *umass_sim; /* SCSI Interface Module */
445 unsigned char flags; /* various device flags */
446 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
448 u_int16_t proto; /* wire and cmd protocol */
449 u_int16_t quirks; /* they got it almost right */
451 usbd_interface_handle iface; /* Mass Storage interface */
452 int ifaceno; /* MS iface number */
454 u_int8_t bulkin; /* bulk-in Endpoint Address */
455 u_int8_t bulkout; /* bulk-out Endpoint Address */
456 u_int8_t intrin; /* intr-in Endp. (CBI) */
457 usbd_pipe_handle bulkin_pipe;
458 usbd_pipe_handle bulkout_pipe;
459 usbd_pipe_handle intrin_pipe;
461 /* Reset the device in a wire protocol specific way */
462 wire_reset_f reset;
464 /* The start of a wire transfer. It prepares the whole transfer (cmd,
465 * data, and status stage) and initiates it. It is up to the state
466 * machine (below) to handle the various stages and errors in these
468 wire_transfer_f transfer;
470 /* The state machine, handling the various states during a transfer */
471 wire_state_f state;
473 /* The command transform function is used to conver the SCSI commands
474 * into their derivatives, like UFI, ATAPI, and friends.
476 command_transform_f transform; /* command transform */
478 /* Bulk specific variables for transfers in progress */
479 umass_bbb_cbw_t cbw; /* command block wrapper */
480 umass_bbb_csw_t csw; /* command status wrapper*/
481 /* CBI specific variables for transfers in progress */
482 umass_cbi_cbl_t cbl; /* command block */
483 umass_cbi_sbl_t sbl; /* status block */
485 /* generic variables for transfers in progress */
486 /* ctrl transfer requests */
487 usb_device_request_t request;
489 /* xfer handles
490 * Most of our operations are initiated from interrupt context, so
491 * we need to avoid using the one that is in use. We want to avoid
492 * allocating them in the interrupt context as well.
494 /* indices into array below */
495 # define XFER_BBB_CBW 0 /* Bulk-Only */
496 # define XFER_BBB_DATA 1
497 # define XFER_BBB_DCLEAR 2
498 # define XFER_BBB_CSW1 3
499 # define XFER_BBB_CSW2 4
500 # define XFER_BBB_SCLEAR 5
501 # define XFER_BBB_RESET1 6
502 # define XFER_BBB_RESET2 7
503 # define XFER_BBB_RESET3 8
505 # define XFER_CBI_CB 0 /* CBI */
506 # define XFER_CBI_DATA 1
507 # define XFER_CBI_STATUS 2
508 # define XFER_CBI_DCLEAR 3
509 # define XFER_CBI_SCLEAR 4
510 # define XFER_CBI_RESET1 5
511 # define XFER_CBI_RESET2 6
512 # define XFER_CBI_RESET3 7
514 # define XFER_NR 9 /* maximum number */
516 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
518 int transfer_dir; /* data direction */
519 void *transfer_data; /* data buffer */
520 int transfer_datalen; /* (maximum) length */
521 int transfer_actlen; /* actual length */
522 transfer_cb_f transfer_cb; /* callback */
523 void *transfer_priv; /* for callback */
524 int transfer_status;
526 int transfer_state;
527 # define TSTATE_ATTACH 0 /* in attach */
528 # define TSTATE_IDLE 1
529 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
530 # define TSTATE_BBB_DATA 3 /* Data transfer */
531 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
532 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
533 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
534 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
535 # define TSTATE_BBB_RESET1 8 /* reset command */
536 # define TSTATE_BBB_RESET2 9 /* in clear stall */
537 # define TSTATE_BBB_RESET3 10 /* out clear stall */
538 # define TSTATE_CBI_COMMAND 11 /* command transfer */
539 # define TSTATE_CBI_DATA 12 /* data transfer */
540 # define TSTATE_CBI_STATUS 13 /* status transfer */
541 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
542 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
543 # define TSTATE_CBI_RESET1 16 /* reset command */
544 # define TSTATE_CBI_RESET2 17 /* in clear stall */
545 # define TSTATE_CBI_RESET3 18 /* out clear stall */
546 # define TSTATE_STATES 19 /* # of states above */
549 /* SCSI/CAM specific variables */
550 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
551 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
552 struct scsi_sense cam_scsi_sense;
553 struct scsi_sense cam_scsi_test_unit_ready;
555 int maxlun; /* maximum LUN number */
556 struct callout rescan_timeout;
559 #ifdef USB_DEBUG
560 char *states[TSTATE_STATES+1] = {
561 /* should be kept in sync with the list at transfer_state */
562 "Attach",
563 "Idle",
564 "BBB CBW",
565 "BBB Data",
566 "BBB Data bulk-in/-out clear stall",
567 "BBB CSW, 1st attempt",
568 "BBB CSW bulk-in clear stall",
569 "BBB CSW, 2nd attempt",
570 "BBB Reset",
571 "BBB bulk-in clear stall",
572 "BBB bulk-out clear stall",
573 "CBI Command",
574 "CBI Data",
575 "CBI Status",
576 "CBI Data bulk-in/-out clear stall",
577 "CBI Status intr-in clear stall",
578 "CBI Reset",
579 "CBI bulk-in clear stall",
580 "CBI bulk-out clear stall",
581 NULL
583 #endif
585 /* If device cannot return valid inquiry data, fake it */
586 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
587 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
588 /*additional_length*/ 31, 0, 0, 0
591 /* USB device probe/attach/detach functions */
592 static device_probe_t umass_match;
593 static device_attach_t umass_attach;
594 static device_detach_t umass_detach;
596 static devclass_t umass_devclass;
598 static kobj_method_t umass_methods[] = {
599 DEVMETHOD(device_probe, umass_match),
600 DEVMETHOD(device_attach, umass_attach),
601 DEVMETHOD(device_detach, umass_detach),
602 {0,0},
603 {0,0}
606 static driver_t umass_driver = {
607 "umass",
608 umass_methods,
609 sizeof(struct umass_softc)
612 MODULE_DEPEND(umass, usb, 1, 1, 1);
614 static int umass_match_proto (struct umass_softc *sc,
615 usbd_interface_handle iface,
616 usbd_device_handle udev);
618 /* quirk functions */
619 static void umass_init_shuttle (struct umass_softc *sc);
621 /* generic transfer functions */
622 static usbd_status umass_setup_transfer (struct umass_softc *sc,
623 usbd_pipe_handle pipe,
624 void *buffer, int buflen, int flags,
625 usbd_xfer_handle xfer);
626 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
627 usbd_device_handle udev,
628 usb_device_request_t *req,
629 void *buffer, int buflen, int flags,
630 usbd_xfer_handle xfer);
631 static void umass_clear_endpoint_stall (struct umass_softc *sc,
632 u_int8_t endpt, usbd_pipe_handle pipe,
633 int state, usbd_xfer_handle xfer);
634 static void umass_reset (struct umass_softc *sc,
635 transfer_cb_f cb, void *priv);
637 /* Bulk-Only related functions */
638 static void umass_bbb_reset (struct umass_softc *sc, int status);
639 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
640 void *cmd, int cmdlen,
641 void *data, int datalen, int dir,
642 transfer_cb_f cb, void *priv);
643 static void umass_bbb_state (usbd_xfer_handle xfer,
644 usbd_private_handle priv,
645 usbd_status err);
646 static int umass_bbb_get_max_lun
647 (struct umass_softc *sc);
649 /* CBI related functions */
650 static int umass_cbi_adsc (struct umass_softc *sc,
651 char *buffer, int buflen,
652 usbd_xfer_handle xfer);
653 static void umass_cbi_reset (struct umass_softc *sc, int status);
654 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
655 void *cmd, int cmdlen,
656 void *data, int datalen, int dir,
657 transfer_cb_f cb, void *priv);
658 static void umass_cbi_state (usbd_xfer_handle xfer,
659 usbd_private_handle priv, usbd_status err);
661 /* CAM related functions */
662 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
663 static void umass_cam_poll (struct cam_sim *sim);
665 static void umass_cam_cb (struct umass_softc *sc, void *priv,
666 int residue, int status);
667 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
668 int residue, int status);
669 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
670 int residue, int status);
672 static void umass_cam_rescan_callback
673 (struct cam_periph *periph,union ccb *ccb);
674 static void umass_cam_rescan (void *addr);
676 static int umass_cam_attach_sim (struct umass_softc *sc);
677 static int umass_cam_attach (struct umass_softc *sc);
678 static int umass_cam_detach_sim (struct umass_softc *sc);
681 /* SCSI specific functions */
682 static int umass_scsi_transform (struct umass_softc *sc,
683 unsigned char *cmd, int cmdlen,
684 unsigned char **rcmd, int *rcmdlen);
686 /* UFI specific functions */
687 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
688 static int umass_ufi_transform (struct umass_softc *sc,
689 unsigned char *cmd, int cmdlen,
690 unsigned char **rcmd, int *rcmdlen);
692 /* ATAPI (8070i) specific functions */
693 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
694 static int umass_atapi_transform (struct umass_softc *sc,
695 unsigned char *cmd, int cmdlen,
696 unsigned char **rcmd, int *rcmdlen);
698 /* RBC specific functions */
699 static int umass_rbc_transform (struct umass_softc *sc,
700 unsigned char *cmd, int cmdlen,
701 unsigned char **rcmd, int *rcmdlen);
703 #ifdef USB_DEBUG
704 /* General debugging functions */
705 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
706 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
707 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
708 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
709 int buflen, int printlen);
710 #endif
712 MODULE_DEPEND(umass, cam, 1,1,1);
715 * USB device probe/attach/detach
719 * Match the device we are seeing with the devices supported. Fill in the
720 * description in the softc accordingly. This function is called from both
721 * probe and attach.
724 static int
725 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
726 usbd_device_handle udev)
728 usb_device_descriptor_t *dd;
729 usb_interface_descriptor_t *id;
730 int i;
731 int found = 0;
733 sc->sc_udev = udev;
734 sc->proto = 0;
735 sc->quirks = 0;
737 dd = usbd_get_device_descriptor(udev);
739 /* An entry specifically for Y-E Data devices as they don't fit in the
740 * device description table.
742 if (UGETW(dd->idVendor) == 0x057b && UGETW(dd->idProduct) == 0x0000) {
744 /* Revisions < 1.28 do not handle the inerrupt endpoint
745 * very well.
747 if (UGETW(dd->bcdDevice) < 0x128) {
748 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
749 } else {
750 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
754 * Revisions < 1.28 do not have the TEST UNIT READY command
755 * Revisions == 1.28 have a broken TEST UNIT READY
757 if (UGETW(dd->bcdDevice) <= 0x128)
758 sc->quirks |= NO_TEST_UNIT_READY;
760 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
761 return(UMATCH_VENDOR_PRODUCT);
764 /* Check the list of supported devices for a match. While looking,
765 * check for wildcarded and fully matched. First match wins.
767 for (i = 0; umass_devdescrs[i].vendor != EOT_ID && !found; i++) {
768 if (umass_devdescrs[i].vendor == WILDCARD_ID &&
769 umass_devdescrs[i].product == WILDCARD_ID &&
770 umass_devdescrs[i].release == WILDCARD_ID) {
771 kprintf("umass: ignoring invalid wildcard quirk\n");
772 continue;
774 if ((umass_devdescrs[i].vendor == UGETW(dd->idVendor) ||
775 umass_devdescrs[i].vendor == WILDCARD_ID)
776 && (umass_devdescrs[i].product == UGETW(dd->idProduct) ||
777 umass_devdescrs[i].product == WILDCARD_ID)) {
778 if (umass_devdescrs[i].release == WILDCARD_ID) {
779 sc->proto = umass_devdescrs[i].proto;
780 sc->quirks = umass_devdescrs[i].quirks;
781 return (UMATCH_VENDOR_PRODUCT);
782 } else if (umass_devdescrs[i].release ==
783 UGETW(dd->bcdDevice)) {
784 sc->proto = umass_devdescrs[i].proto;
785 sc->quirks = umass_devdescrs[i].quirks;
786 return (UMATCH_VENDOR_PRODUCT_REV);
787 } /* else RID does not match */
791 /* Check for a standards compliant device */
793 id = usbd_get_interface_descriptor(iface);
794 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
795 return(UMATCH_NONE);
797 switch (id->bInterfaceSubClass) {
798 case UISUBCLASS_SCSI:
799 sc->proto |= UMASS_PROTO_SCSI;
800 break;
801 case UISUBCLASS_UFI:
802 sc->proto |= UMASS_PROTO_UFI;
803 break;
804 case UISUBCLASS_RBC:
805 sc->proto |= UMASS_PROTO_RBC;
806 break;
807 case UISUBCLASS_SFF8020I:
808 case UISUBCLASS_SFF8070I:
809 sc->proto |= UMASS_PROTO_ATAPI;
810 break;
811 default:
812 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
813 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
814 return(UMATCH_NONE);
817 switch (id->bInterfaceProtocol) {
818 case UIPROTO_MASS_CBI:
819 sc->proto |= UMASS_PROTO_CBI;
820 break;
821 case UIPROTO_MASS_CBI_I:
822 sc->proto |= UMASS_PROTO_CBI_I;
823 break;
824 case UIPROTO_MASS_BBB_OLD:
825 case UIPROTO_MASS_BBB:
826 sc->proto |= UMASS_PROTO_BBB;
827 break;
828 default:
829 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
830 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
831 return(UMATCH_NONE);
834 return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
837 static int
838 umass_match(device_t self)
840 struct usb_attach_arg *uaa = device_get_ivars(self);
841 struct umass_softc *sc = device_get_softc(self);
843 sc->sc_dev = self;
845 if (uaa->iface == NULL)
846 return(UMATCH_NONE);
848 return(umass_match_proto(sc, uaa->iface, uaa->device));
851 static int
852 umass_attach(device_t self)
854 struct umass_softc *sc = device_get_softc(self);
855 struct usb_attach_arg *uaa = device_get_ivars(self);
856 usb_interface_descriptor_t *id;
857 usb_endpoint_descriptor_t *ed;
858 int i;
859 int err;
862 * the softc struct is bzero-ed in device_set_driver. We can safely
863 * call umass_detach without specifically initialising the struct.
866 sc->sc_dev = self;
868 sc->iface = uaa->iface;
869 sc->ifaceno = uaa->ifaceno;
871 /* initialise the proto and drive values in the umass_softc (again) */
872 (void) umass_match_proto(sc, sc->iface, uaa->device);
874 id = usbd_get_interface_descriptor(sc->iface);
875 #ifdef USB_DEBUG
876 kprintf("%s: ", device_get_nameunit(sc->sc_dev));
877 switch (sc->proto&UMASS_PROTO_COMMAND) {
878 case UMASS_PROTO_SCSI:
879 kprintf("SCSI");
880 break;
881 case UMASS_PROTO_ATAPI:
882 kprintf("8070i (ATAPI)");
883 break;
884 case UMASS_PROTO_UFI:
885 kprintf("UFI");
886 break;
887 case UMASS_PROTO_RBC:
888 kprintf("RBC");
889 break;
890 default:
891 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
892 break;
894 kprintf(" over ");
895 switch (sc->proto&UMASS_PROTO_WIRE) {
896 case UMASS_PROTO_BBB:
897 kprintf("Bulk-Only");
898 break;
899 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
900 kprintf("CBI");
901 break;
902 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
903 kprintf("CBI with CCI");
904 #ifndef CBI_I
905 kprintf(" (using CBI)");
906 #endif
907 break;
908 default:
909 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
911 kprintf("; quirks = 0x%04x\n", sc->quirks);
912 #endif
914 #ifndef CBI_I
915 if (sc->proto & UMASS_PROTO_CBI_I) {
916 /* See beginning of file for comment on the use of CBI with CCI */
917 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
919 #endif
921 if (sc->quirks & ALT_IFACE_1) {
922 err = usbd_set_interface(0, 1);
923 if (err) {
924 DPRINTF(UDMASS_USB, ("%s: could not switch to "
925 "Alt Interface %d\n",
926 device_get_nameunit(sc->sc_dev), 1));
927 umass_detach(self);
928 return ENXIO;
933 * In addition to the Control endpoint the following endpoints
934 * are required:
935 * a) bulk-in endpoint.
936 * b) bulk-out endpoint.
937 * and for Control/Bulk/Interrupt with CCI (CBI_I)
938 * c) intr-in
940 * The endpoint addresses are not fixed, so we have to read them
941 * from the device descriptors of the current interface.
943 for (i = 0 ; i < id->bNumEndpoints ; i++) {
944 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
945 if (!ed) {
946 kprintf("%s: could not read endpoint descriptor\n",
947 device_get_nameunit(sc->sc_dev));
948 return ENXIO;
950 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
951 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
952 sc->bulkin = ed->bEndpointAddress;
953 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
954 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
955 sc->bulkout = ed->bEndpointAddress;
956 } else if (sc->proto & UMASS_PROTO_CBI_I
957 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
958 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
959 sc->intrin = ed->bEndpointAddress;
960 #ifdef USB_DEBUG
961 if (UGETW(ed->wMaxPacketSize) > 2) {
962 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
963 device_get_nameunit(sc->sc_dev),
964 UGETW(ed->wMaxPacketSize)));
966 #endif
970 /* check whether we found all the endpoints we need */
971 if (!sc->bulkin || !sc->bulkout
972 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
973 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
974 device_get_nameunit(sc->sc_dev),
975 sc->bulkin, sc->bulkout, sc->intrin));
976 umass_detach(self);
977 return ENXIO;
980 /* Open the bulk-in and -out pipe */
981 err = usbd_open_pipe(sc->iface, sc->bulkout,
982 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
983 if (err) {
984 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
985 device_get_nameunit(sc->sc_dev), sc->bulkout));
986 umass_detach(self);
987 return ENXIO;
989 err = usbd_open_pipe(sc->iface, sc->bulkin,
990 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
991 if (err) {
992 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
993 device_get_nameunit(sc->sc_dev), sc->bulkin));
994 umass_detach(self);
995 return ENXIO;
997 /* Open the intr-in pipe if the protocol is CBI with CCI.
998 * Note: early versions of the Zip drive do have an interrupt pipe, but
999 * this pipe is unused
1001 * We do not open the interrupt pipe as an interrupt pipe, but as a
1002 * normal bulk endpoint. We send an IN transfer down the wire at the
1003 * appropriate time, because we know exactly when to expect data on
1004 * that endpoint. This saves bandwidth, but more important, makes the
1005 * code for handling the data on that endpoint simpler. No data
1006 * arriving concurently.
1008 if (sc->proto & UMASS_PROTO_CBI_I) {
1009 err = usbd_open_pipe(sc->iface, sc->intrin,
1010 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1011 if (err) {
1012 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1013 device_get_nameunit(sc->sc_dev), sc->intrin));
1014 umass_detach(self);
1015 return ENXIO;
1019 /* initialisation of generic part */
1020 sc->transfer_state = TSTATE_ATTACH;
1022 /* request a sufficient number of xfer handles */
1023 for (i = 0; i < XFER_NR; i++) {
1024 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1025 if (!sc->transfer_xfer[i]) {
1026 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1027 device_get_nameunit(sc->sc_dev)));
1028 umass_detach(self);
1029 return ENXIO;
1033 /* Initialise the wire protocol specific methods */
1034 if (sc->proto & UMASS_PROTO_BBB) {
1035 sc->reset = umass_bbb_reset;
1036 sc->transfer = umass_bbb_transfer;
1037 sc->state = umass_bbb_state;
1038 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1039 sc->reset = umass_cbi_reset;
1040 sc->transfer = umass_cbi_transfer;
1041 sc->state = umass_cbi_state;
1042 #ifdef USB_DEBUG
1043 } else {
1044 panic("%s:%d: Unknown proto 0x%02x",
1045 __FILE__, __LINE__, sc->proto);
1046 #endif
1049 if (sc->proto & UMASS_PROTO_SCSI)
1050 sc->transform = umass_scsi_transform;
1051 else if (sc->proto & UMASS_PROTO_UFI)
1052 sc->transform = umass_ufi_transform;
1053 else if (sc->proto & UMASS_PROTO_ATAPI)
1054 sc->transform = umass_atapi_transform;
1055 else if (sc->proto & UMASS_PROTO_RBC)
1056 sc->transform = umass_rbc_transform;
1057 #ifdef USB_DEBUG
1058 else
1059 panic("No transformation defined for command proto 0x%02x",
1060 sc->proto & UMASS_PROTO_COMMAND);
1061 #endif
1063 /* From here onwards the device can be used. */
1065 if (sc->quirks & SHUTTLE_INIT)
1066 umass_init_shuttle(sc);
1068 /* Get the maximum LUN supported by the device.
1070 if ((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB)
1071 sc->maxlun = umass_bbb_get_max_lun(sc);
1072 else
1073 sc->maxlun = 0;
1075 if ((sc->proto & UMASS_PROTO_SCSI) ||
1076 (sc->proto & UMASS_PROTO_ATAPI) ||
1077 (sc->proto & UMASS_PROTO_UFI) ||
1078 (sc->proto & UMASS_PROTO_RBC)) {
1079 /* Prepare the SCSI command block */
1080 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1081 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1083 /* register the SIM */
1084 err = umass_cam_attach_sim(sc);
1085 if (err) {
1086 umass_detach(self);
1087 return ENXIO;
1089 /* scan the new sim */
1090 err = umass_cam_attach(sc);
1091 if (err) {
1092 umass_cam_detach_sim(sc);
1093 umass_detach(self);
1094 return ENXIO;
1096 } else {
1097 panic("%s:%d: Unknown proto 0x%02x",
1098 __FILE__, __LINE__, sc->proto);
1101 sc->transfer_state = TSTATE_IDLE;
1102 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1104 return 0;
1107 static int
1108 umass_detach(device_t self)
1110 struct umass_softc *sc = device_get_softc(self);
1111 int err = 0;
1112 int i;
1113 int to;
1115 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1118 * Set UMASS_FLAGS_GONE to prevent any new transfers from being
1119 * queued, and abort any transfers in progress to ensure that
1120 * pending requests (e.g. from CAM's bus scan) are terminated.
1122 sc->flags |= UMASS_FLAGS_GONE;
1124 if (sc->bulkout_pipe)
1125 usbd_abort_pipe(sc->bulkout_pipe);
1126 if (sc->bulkin_pipe)
1127 usbd_abort_pipe(sc->bulkin_pipe);
1128 if (sc->intrin_pipe)
1129 usbd_abort_pipe(sc->intrin_pipe);
1132 * Wait until we go idle to make sure that all of our xfer requests
1133 * have finished. We could be in the middle of a BBB reset (which
1134 * would not be effected by the pipe aborts above).
1136 to = hz;
1137 while (sc->transfer_state != TSTATE_IDLE) {
1138 kprintf("%s: state %d waiting for idle\n",
1139 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1140 tsleep(sc, 0, "umassidl", to);
1141 if (to >= hz * 10) {
1142 kprintf("%s: state %d giving up!\n",
1143 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1144 break;
1146 to += hz;
1149 if ((sc->proto & UMASS_PROTO_SCSI) ||
1150 (sc->proto & UMASS_PROTO_ATAPI) ||
1151 (sc->proto & UMASS_PROTO_UFI) ||
1152 (sc->proto & UMASS_PROTO_RBC)) {
1153 /* detach the SCSI host controller (SIM) */
1154 err = umass_cam_detach_sim(sc);
1157 for (i = 0; i < XFER_NR; i++) {
1158 if (sc->transfer_xfer[i])
1159 usbd_free_xfer(sc->transfer_xfer[i]);
1162 /* remove all the pipes */
1163 if (sc->bulkout_pipe)
1164 usbd_close_pipe(sc->bulkout_pipe);
1165 if (sc->bulkin_pipe)
1166 usbd_close_pipe(sc->bulkin_pipe);
1167 if (sc->intrin_pipe)
1168 usbd_close_pipe(sc->intrin_pipe);
1170 return(err);
1173 static void
1174 umass_init_shuttle(struct umass_softc *sc)
1176 usb_device_request_t req;
1177 u_char status[2];
1179 /* The Linux driver does this, but no one can tell us what the
1180 * command does.
1182 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1183 req.bRequest = 1; /* XXX unknown command */
1184 USETW(req.wValue, 0);
1185 USETW(req.wIndex, sc->ifaceno);
1186 USETW(req.wLength, sizeof status);
1187 (void) usbd_do_request(sc->sc_udev, &req, &status);
1189 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1190 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1194 * Generic functions to handle transfers
1197 static usbd_status
1198 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1199 void *buffer, int buflen, int flags,
1200 usbd_xfer_handle xfer)
1202 usbd_status err;
1204 /* Initialiase a USB transfer and then schedule it */
1206 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1207 UMASS_TIMEOUT, sc->state);
1209 err = usbd_transfer(xfer);
1210 if (err && err != USBD_IN_PROGRESS) {
1211 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1212 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1213 return(err);
1216 return (USBD_NORMAL_COMPLETION);
1220 static usbd_status
1221 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1222 usb_device_request_t *req,
1223 void *buffer, int buflen, int flags,
1224 usbd_xfer_handle xfer)
1226 usbd_status err;
1228 /* Initialiase a USB control transfer and then schedule it */
1230 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1231 UMASS_TIMEOUT, req, buffer, buflen, flags, sc->state);
1233 err = usbd_transfer(xfer);
1234 if (err && err != USBD_IN_PROGRESS) {
1235 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1236 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1238 /* do not reset, as this would make us loop */
1239 return(err);
1242 return (USBD_NORMAL_COMPLETION);
1245 static void
1246 umass_clear_endpoint_stall(struct umass_softc *sc,
1247 u_int8_t endpt, usbd_pipe_handle pipe,
1248 int state, usbd_xfer_handle xfer)
1250 usbd_device_handle udev;
1252 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1253 device_get_nameunit(sc->sc_dev), endpt));
1255 usbd_interface2device_handle(sc->iface, &udev);
1257 sc->transfer_state = state;
1259 usbd_clear_endpoint_toggle(pipe);
1261 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1262 sc->request.bRequest = UR_CLEAR_FEATURE;
1263 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1264 USETW(sc->request.wIndex, endpt);
1265 USETW(sc->request.wLength, 0);
1266 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1269 static void
1270 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1272 sc->transfer_cb = cb;
1273 sc->transfer_priv = priv;
1275 /* The reset is a forced reset, so no error (yet) */
1276 sc->reset(sc, STATUS_CMD_OK);
1280 * Bulk protocol specific functions
1283 static void
1284 umass_bbb_reset(struct umass_softc *sc, int status)
1286 usbd_device_handle udev;
1288 KASSERT(sc->proto & UMASS_PROTO_BBB,
1289 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x\n",
1290 device_get_nameunit(sc->sc_dev), sc->proto));
1293 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1295 * For Reset Recovery the host shall issue in the following order:
1296 * a) a Bulk-Only Mass Storage Reset
1297 * b) a Clear Feature HALT to the Bulk-In endpoint
1298 * c) a Clear Feature HALT to the Bulk-Out endpoint
1300 * This is done in 3 steps, states:
1301 * TSTATE_BBB_RESET1
1302 * TSTATE_BBB_RESET2
1303 * TSTATE_BBB_RESET3
1305 * If the reset doesn't succeed, the device should be port reset.
1308 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1309 device_get_nameunit(sc->sc_dev)));
1311 sc->transfer_state = TSTATE_BBB_RESET1;
1312 sc->transfer_status = status;
1314 usbd_interface2device_handle(sc->iface, &udev);
1316 /* reset is a class specific interface write */
1317 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1318 sc->request.bRequest = UR_BBB_RESET;
1319 USETW(sc->request.wValue, 0);
1320 USETW(sc->request.wIndex, sc->ifaceno);
1321 USETW(sc->request.wLength, 0);
1322 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1323 sc->transfer_xfer[XFER_BBB_RESET1]);
1326 static void
1327 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1328 void *data, int datalen, int dir,
1329 transfer_cb_f cb, void *priv)
1331 KASSERT(sc->proto & UMASS_PROTO_BBB,
1332 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x\n",
1333 device_get_nameunit(sc->sc_dev), sc->proto));
1336 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1337 * a data phase of datalen bytes from/to the device and finally a
1338 * csw read phase.
1339 * If the data direction was inbound a maximum of datalen bytes
1340 * is stored in the buffer pointed to by data.
1342 * umass_bbb_transfer initialises the transfer and lets the state
1343 * machine in umass_bbb_state handle the completion. It uses the
1344 * following states:
1345 * TSTATE_BBB_COMMAND
1346 * -> TSTATE_BBB_DATA
1347 * -> TSTATE_BBB_STATUS
1348 * -> TSTATE_BBB_STATUS2
1349 * -> TSTATE_BBB_IDLE
1351 * An error in any of those states will invoke
1352 * umass_bbb_reset.
1355 /* check the given arguments */
1356 KASSERT(datalen == 0 || data != NULL,
1357 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1358 KASSERT(cmdlen <= CBWCDBLENGTH,
1359 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1360 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1361 KASSERT(dir == DIR_NONE || datalen > 0,
1362 ("%s: datalen == 0 while direction is not NONE\n",
1363 device_get_nameunit(sc->sc_dev)));
1364 KASSERT(datalen == 0 || dir != DIR_NONE,
1365 ("%s: direction is NONE while datalen is not zero\n",
1366 device_get_nameunit(sc->sc_dev)));
1367 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1368 ("%s: CBW struct does not have the right size (%ld vs. %d)\n",
1369 device_get_nameunit(sc->sc_dev),
1370 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1371 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1372 ("%s: CSW struct does not have the right size (%ld vs. %d)\n",
1373 device_get_nameunit(sc->sc_dev),
1374 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1377 * Determine the direction of the data transfer and the length.
1379 * dCBWDataTransferLength (datalen) :
1380 * This field indicates the number of bytes of data that the host
1381 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1382 * the Direction bit) during the execution of this command. If this
1383 * field is set to 0, the device will expect that no data will be
1384 * transferred IN or OUT during this command, regardless of the value
1385 * of the Direction bit defined in dCBWFlags.
1387 * dCBWFlags (dir) :
1388 * The bits of the Flags field are defined as follows:
1389 * Bits 0-6 reserved
1390 * Bit 7 Direction - this bit shall be ignored if the
1391 * dCBWDataTransferLength field is zero.
1392 * 0 = data Out from host to device
1393 * 1 = data In from device to host
1396 /* Fill in the Command Block Wrapper
1397 * We fill in all the fields, so there is no need to bzero it first.
1399 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1400 /* We don't care about the initial value, as long as the values are unique */
1401 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1402 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1403 /* DIR_NONE is treated as DIR_OUT (0x00) */
1404 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1405 sc->cbw.bCBWLUN = lun;
1406 sc->cbw.bCDBLength = cmdlen;
1407 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1409 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1411 /* store the details for the data transfer phase */
1412 sc->transfer_dir = dir;
1413 sc->transfer_data = data;
1414 sc->transfer_datalen = datalen;
1415 sc->transfer_actlen = 0;
1416 sc->transfer_cb = cb;
1417 sc->transfer_priv = priv;
1418 sc->transfer_status = STATUS_CMD_OK;
1420 /* move from idle to the command state */
1421 sc->transfer_state = TSTATE_BBB_COMMAND;
1423 /* Send the CBW from host to device via bulk-out endpoint. */
1424 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1425 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1426 sc->transfer_xfer[XFER_BBB_CBW])) {
1427 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1432 static void
1433 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1434 usbd_status err)
1436 struct umass_softc *sc = (struct umass_softc *) priv;
1437 usbd_xfer_handle next_xfer;
1439 KASSERT(sc->proto & UMASS_PROTO_BBB,
1440 ("%s: umass_bbb_state: wrong sc->proto 0x%02x\n",
1441 device_get_nameunit(sc->sc_dev), sc->proto));
1444 * State handling for BBB transfers.
1446 * The subroutine is rather long. It steps through the states given in
1447 * Annex A of the Bulk-Only specification.
1448 * Each state first does the error handling of the previous transfer
1449 * and then prepares the next transfer.
1450 * Each transfer is done asynchroneously so after the request/transfer
1451 * has been submitted you will find a 'return;'.
1454 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1455 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1456 states[sc->transfer_state], xfer, usbd_errstr(err)));
1458 switch (sc->transfer_state) {
1460 /***** Bulk Transfer *****/
1461 case TSTATE_BBB_COMMAND:
1462 /* Command transport phase, error handling */
1463 if (err) {
1464 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1465 device_get_nameunit(sc->sc_dev)));
1466 /* If the device detects that the CBW is invalid, then
1467 * the device may STALL both bulk endpoints and require
1468 * a Bulk-Reset
1470 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1471 return;
1474 /* Data transport phase, setup transfer */
1475 sc->transfer_state = TSTATE_BBB_DATA;
1476 if (sc->transfer_dir == DIR_IN) {
1477 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1478 sc->transfer_data, sc->transfer_datalen,
1479 USBD_SHORT_XFER_OK,
1480 sc->transfer_xfer[XFER_BBB_DATA]))
1481 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1483 return;
1484 } else if (sc->transfer_dir == DIR_OUT) {
1485 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1486 sc->transfer_data, sc->transfer_datalen,
1487 0, /* fixed length transfer */
1488 sc->transfer_xfer[XFER_BBB_DATA]))
1489 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1491 return;
1492 } else {
1493 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1494 device_get_nameunit(sc->sc_dev)));
1497 /* FALLTHROUGH if no data phase, err == 0 */
1498 case TSTATE_BBB_DATA:
1499 /* Command transport phase, error handling (ignored if no data
1500 * phase (fallthrough from previous state)) */
1501 if (sc->transfer_dir != DIR_NONE) {
1502 /* retrieve the length of the transfer that was done */
1503 usbd_get_xfer_status(xfer, NULL, NULL,
1504 &sc->transfer_actlen, NULL);
1506 if (err) {
1507 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
1508 "%s\n", device_get_nameunit(sc->sc_dev),
1509 (sc->transfer_dir == DIR_IN?"in":"out"),
1510 sc->transfer_datalen,usbd_errstr(err)));
1512 if (err == USBD_STALLED) {
1513 umass_clear_endpoint_stall(sc,
1514 (sc->transfer_dir == DIR_IN?
1515 sc->bulkin:sc->bulkout),
1516 (sc->transfer_dir == DIR_IN?
1517 sc->bulkin_pipe:sc->bulkout_pipe),
1518 TSTATE_BBB_DCLEAR,
1519 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1520 return;
1521 } else {
1522 /* Unless the error is a pipe stall the
1523 * error is fatal.
1525 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1526 return;
1531 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1532 umass_dump_buffer(sc, sc->transfer_data,
1533 sc->transfer_datalen, 48));
1537 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1538 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1539 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1540 /* Reading of CSW after bulk stall condition in data phase
1541 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1542 * reading CSW (TSTATE_BBB_SCLEAR).
1543 * In the case of no data phase or successfull data phase,
1544 * err == 0 and the following if block is passed.
1546 if (err) { /* should not occur */
1547 /* try the transfer below, even if clear stall failed */
1548 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1549 ", %s\n", device_get_nameunit(sc->sc_dev),
1550 (sc->transfer_dir == DIR_IN? "in":"out"),
1551 usbd_errstr(err)));
1552 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1553 return;
1556 /* Status transport phase, setup transfer */
1557 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1558 sc->transfer_state == TSTATE_BBB_DATA ||
1559 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1560 /* After no data phase, successfull data phase and
1561 * after clearing bulk-in/-out stall condition
1563 sc->transfer_state = TSTATE_BBB_STATUS1;
1564 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1565 } else {
1566 /* After first attempt of fetching CSW */
1567 sc->transfer_state = TSTATE_BBB_STATUS2;
1568 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1571 /* Read the Command Status Wrapper via bulk-in endpoint. */
1572 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1573 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
1574 next_xfer)) {
1575 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1576 return;
1579 return;
1580 case TSTATE_BBB_STATUS1: /* first attempt */
1581 case TSTATE_BBB_STATUS2: /* second attempt */
1582 /* Status transfer, error handling */
1584 int Residue;
1585 if (err) {
1586 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1587 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
1588 (sc->transfer_state == TSTATE_BBB_STATUS1?
1589 ", retrying":"")));
1591 /* If this was the first attempt at fetching the CSW
1592 * retry it, otherwise fail.
1594 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1595 umass_clear_endpoint_stall(sc,
1596 sc->bulkin, sc->bulkin_pipe,
1597 TSTATE_BBB_SCLEAR,
1598 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1599 return;
1600 } else {
1601 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1602 return;
1606 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1608 /* Translate weird command-status signatures. */
1609 if ((sc->quirks & WRONG_CSWSIG) &&
1610 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
1611 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1613 Residue = UGETDW(sc->csw.dCSWDataResidue);
1614 if (Residue == 0 &&
1615 sc->transfer_datalen - sc->transfer_actlen != 0)
1616 Residue = sc->transfer_datalen - sc->transfer_actlen;
1618 /* Check CSW and handle any error */
1619 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1620 /* Invalid CSW: Wrong signature or wrong tag might
1621 * indicate that the device is confused -> reset it.
1623 kprintf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1624 device_get_nameunit(sc->sc_dev),
1625 UGETDW(sc->csw.dCSWSignature),
1626 CSWSIGNATURE);
1628 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1629 return;
1630 } else if (UGETDW(sc->csw.dCSWTag)
1631 != UGETDW(sc->cbw.dCBWTag)) {
1632 kprintf("%s: Invalid CSW: tag %d should be %d\n",
1633 device_get_nameunit(sc->sc_dev),
1634 UGETDW(sc->csw.dCSWTag),
1635 UGETDW(sc->cbw.dCBWTag));
1637 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1638 return;
1640 /* CSW is valid here */
1641 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1642 kprintf("%s: Invalid CSW: status %d > %d\n",
1643 device_get_nameunit(sc->sc_dev),
1644 sc->csw.bCSWStatus,
1645 CSWSTATUS_PHASE);
1647 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1648 return;
1649 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1650 kprintf("%s: Phase Error, residue = %d\n",
1651 device_get_nameunit(sc->sc_dev), Residue);
1653 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1654 return;
1656 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1657 /* Buffer overrun! Don't let this go by unnoticed */
1658 panic("%s: transferred %db instead of %db",
1659 device_get_nameunit(sc->sc_dev),
1660 sc->transfer_actlen, sc->transfer_datalen);
1662 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1663 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1664 device_get_nameunit(sc->sc_dev), Residue));
1666 /* SCSI command failed but transfer was succesful */
1667 sc->transfer_state = TSTATE_IDLE;
1668 sc->transfer_cb(sc, sc->transfer_priv, Residue,
1669 STATUS_CMD_FAILED);
1670 return;
1672 } else { /* success */
1673 sc->transfer_state = TSTATE_IDLE;
1674 sc->transfer_cb(sc, sc->transfer_priv, Residue,
1675 STATUS_CMD_OK);
1677 return;
1681 /***** Bulk Reset *****/
1682 case TSTATE_BBB_RESET1:
1683 if (err)
1684 kprintf("%s: BBB reset failed, %s\n",
1685 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
1687 umass_clear_endpoint_stall(sc,
1688 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
1689 sc->transfer_xfer[XFER_BBB_RESET2]);
1691 return;
1692 case TSTATE_BBB_RESET2:
1693 if (err) /* should not occur */
1694 kprintf("%s: BBB bulk-in clear stall failed, %s\n",
1695 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
1696 /* no error recovery, otherwise we end up in a loop */
1698 umass_clear_endpoint_stall(sc,
1699 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
1700 sc->transfer_xfer[XFER_BBB_RESET3]);
1702 return;
1703 case TSTATE_BBB_RESET3:
1704 if (err) /* should not occur */
1705 kprintf("%s: BBB bulk-out clear stall failed, %s\n",
1706 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
1707 /* no error recovery, otherwise we end up in a loop */
1709 sc->transfer_state = TSTATE_IDLE;
1710 if (sc->transfer_priv) {
1711 sc->transfer_cb(sc, sc->transfer_priv,
1712 sc->transfer_datalen,
1713 sc->transfer_status);
1716 return;
1718 /***** Default *****/
1719 default:
1720 panic("%s: Unknown state %d",
1721 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1725 static int
1726 umass_bbb_get_max_lun(struct umass_softc *sc)
1728 usbd_device_handle udev;
1729 usb_device_request_t req;
1730 usbd_status err;
1731 usb_interface_descriptor_t *id;
1732 int maxlun = 0;
1733 u_int8_t buf = 0;
1735 usbd_interface2device_handle(sc->iface, &udev);
1736 id = usbd_get_interface_descriptor(sc->iface);
1738 /* The Get Max Lun command is a class-specific request. */
1739 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1740 req.bRequest = UR_BBB_GET_MAX_LUN;
1741 USETW(req.wValue, 0);
1742 USETW(req.wIndex, id->bInterfaceNumber);
1743 USETW(req.wLength, 1);
1745 err = usbd_do_request(udev, &req, &buf);
1746 switch (err) {
1747 case USBD_NORMAL_COMPLETION:
1748 maxlun = buf;
1749 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
1750 device_get_nameunit(sc->sc_dev), maxlun));
1751 break;
1752 case USBD_STALLED:
1753 case USBD_SHORT_XFER:
1754 default:
1755 /* Device doesn't support Get Max Lun request. */
1756 kprintf("%s: Get Max Lun not supported (%s)\n",
1757 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
1758 /* XXX Should we port_reset the device? */
1759 break;
1762 return(maxlun);
1766 * Command/Bulk/Interrupt (CBI) specific functions
1769 static int
1770 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1771 usbd_xfer_handle xfer)
1773 usbd_device_handle udev;
1775 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
1776 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x\n",
1777 device_get_nameunit(sc->sc_dev), sc->proto));
1779 usbd_interface2device_handle(sc->iface, &udev);
1781 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1782 sc->request.bRequest = UR_CBI_ADSC;
1783 USETW(sc->request.wValue, 0);
1784 USETW(sc->request.wIndex, sc->ifaceno);
1785 USETW(sc->request.wLength, buflen);
1786 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
1787 buflen, 0, xfer);
1791 static void
1792 umass_cbi_reset(struct umass_softc *sc, int status)
1794 int i;
1795 # define SEND_DIAGNOSTIC_CMDLEN 12
1797 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
1798 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x\n",
1799 device_get_nameunit(sc->sc_dev), sc->proto));
1802 * Command Block Reset Protocol
1804 * First send a reset request to the device. Then clear
1805 * any possibly stalled bulk endpoints.
1807 * This is done in 3 steps, states:
1808 * TSTATE_CBI_RESET1
1809 * TSTATE_CBI_RESET2
1810 * TSTATE_CBI_RESET3
1812 * If the reset doesn't succeed, the device should be port reset.
1815 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1816 device_get_nameunit(sc->sc_dev)));
1818 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1819 ("%s: CBL struct is too small (%ld < %d)\n",
1820 device_get_nameunit(sc->sc_dev),
1821 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1823 sc->transfer_state = TSTATE_CBI_RESET1;
1824 sc->transfer_status = status;
1826 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1827 * the two the last 10 bytes of the cbl is filled with 0xff (section
1828 * 2.2 of the CBI spec).
1830 sc->cbl[0] = 0x1d; /* Command Block Reset */
1831 sc->cbl[1] = 0x04;
1832 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1833 sc->cbl[i] = 0xff;
1835 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1836 sc->transfer_xfer[XFER_CBI_RESET1]);
1837 /* XXX if the command fails we should reset the port on the bub */
1840 static void
1841 umass_cbi_transfer(struct umass_softc *sc, int lun,
1842 void *cmd, int cmdlen, void *data, int datalen, int dir,
1843 transfer_cb_f cb, void *priv)
1845 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
1846 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x\n",
1847 device_get_nameunit(sc->sc_dev), sc->proto));
1850 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1851 * a data phase of datalen bytes from/to the device and finally a
1852 * csw read phase.
1853 * If the data direction was inbound a maximum of datalen bytes
1854 * is stored in the buffer pointed to by data.
1856 * umass_cbi_transfer initialises the transfer and lets the state
1857 * machine in umass_cbi_state handle the completion. It uses the
1858 * following states:
1859 * TSTATE_CBI_COMMAND
1860 * -> XXX fill in
1862 * An error in any of those states will invoke
1863 * umass_cbi_reset.
1866 /* check the given arguments */
1867 KASSERT(datalen == 0 || data != NULL,
1868 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1869 KASSERT(datalen == 0 || dir != DIR_NONE,
1870 ("%s: direction is NONE while datalen is not zero\n",
1871 device_get_nameunit(sc->sc_dev)));
1873 /* store the details for the data transfer phase */
1874 sc->transfer_dir = dir;
1875 sc->transfer_data = data;
1876 sc->transfer_datalen = datalen;
1877 sc->transfer_actlen = 0;
1878 sc->transfer_cb = cb;
1879 sc->transfer_priv = priv;
1880 sc->transfer_status = STATUS_CMD_OK;
1882 /* move from idle to the command state */
1883 sc->transfer_state = TSTATE_CBI_COMMAND;
1885 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
1887 /* Send the Command Block from host to device via control endpoint. */
1888 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1889 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1892 static void
1893 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1894 usbd_status err)
1896 struct umass_softc *sc = (struct umass_softc *) priv;
1898 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
1899 ("%s: umass_cbi_state: wrong sc->proto 0x%02x\n",
1900 device_get_nameunit(sc->sc_dev), sc->proto));
1903 * State handling for CBI transfers.
1906 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1907 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1908 states[sc->transfer_state], xfer, usbd_errstr(err)));
1910 switch (sc->transfer_state) {
1912 /***** CBI Transfer *****/
1913 case TSTATE_CBI_COMMAND:
1914 if (err == USBD_STALLED) {
1915 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1916 device_get_nameunit(sc->sc_dev)));
1917 /* Status transport by control pipe (section 2.3.2.1).
1918 * The command contained in the command block failed.
1920 * The control pipe has already been unstalled by the
1921 * USB stack.
1922 * Section 2.4.3.1.1 states that the bulk in endpoints
1923 * should not be stalled at this point.
1926 sc->transfer_state = TSTATE_IDLE;
1927 sc->transfer_cb(sc, sc->transfer_priv,
1928 sc->transfer_datalen,
1929 STATUS_CMD_FAILED);
1931 return;
1932 } else if (err) {
1933 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1934 device_get_nameunit(sc->sc_dev)));
1935 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1937 return;
1940 sc->transfer_state = TSTATE_CBI_DATA;
1941 if (sc->transfer_dir == DIR_IN) {
1942 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1943 sc->transfer_data, sc->transfer_datalen,
1944 USBD_SHORT_XFER_OK,
1945 sc->transfer_xfer[XFER_CBI_DATA]))
1946 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1948 } else if (sc->transfer_dir == DIR_OUT) {
1949 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1950 sc->transfer_data, sc->transfer_datalen,
1951 0, /* fixed length transfer */
1952 sc->transfer_xfer[XFER_CBI_DATA]))
1953 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1955 } else if (sc->proto & UMASS_PROTO_CBI_I) {
1956 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1957 device_get_nameunit(sc->sc_dev)));
1958 sc->transfer_state = TSTATE_CBI_STATUS;
1959 if (umass_setup_transfer(sc, sc->intrin_pipe,
1960 &sc->sbl, sizeof(sc->sbl),
1961 0, /* fixed length transfer */
1962 sc->transfer_xfer[XFER_CBI_STATUS])){
1963 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1965 } else {
1966 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1967 device_get_nameunit(sc->sc_dev)));
1968 /* No command completion interrupt. Request
1969 * sense data.
1971 sc->transfer_state = TSTATE_IDLE;
1972 sc->transfer_cb(sc, sc->transfer_priv,
1973 0, STATUS_CMD_UNKNOWN);
1976 return;
1978 case TSTATE_CBI_DATA:
1979 /* retrieve the length of the transfer that was done */
1980 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
1982 if (err) {
1983 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
1984 "%s\n", device_get_nameunit(sc->sc_dev),
1985 (sc->transfer_dir == DIR_IN?"in":"out"),
1986 sc->transfer_datalen,usbd_errstr(err)));
1988 if (err == USBD_STALLED) {
1989 umass_clear_endpoint_stall(sc,
1990 sc->bulkin, sc->bulkin_pipe,
1991 TSTATE_CBI_DCLEAR,
1992 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1993 } else {
1994 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1996 return;
1999 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2000 umass_dump_buffer(sc, sc->transfer_data,
2001 sc->transfer_actlen, 48));
2003 if (sc->proto & UMASS_PROTO_CBI_I) {
2004 sc->transfer_state = TSTATE_CBI_STATUS;
2005 if (umass_setup_transfer(sc, sc->intrin_pipe,
2006 &sc->sbl, sizeof(sc->sbl),
2007 0, /* fixed length transfer */
2008 sc->transfer_xfer[XFER_CBI_STATUS])){
2009 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2011 } else {
2012 /* No command completion interrupt. Request
2013 * sense to get status of command.
2015 sc->transfer_state = TSTATE_IDLE;
2016 sc->transfer_cb(sc, sc->transfer_priv,
2017 sc->transfer_datalen - sc->transfer_actlen,
2018 STATUS_CMD_UNKNOWN);
2020 return;
2022 case TSTATE_CBI_STATUS:
2023 if (err) {
2024 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2025 device_get_nameunit(sc->sc_dev)));
2026 /* Status transport by interrupt pipe (section 2.3.2.2).
2029 if (err == USBD_STALLED) {
2030 umass_clear_endpoint_stall(sc,
2031 sc->intrin, sc->intrin_pipe,
2032 TSTATE_CBI_SCLEAR,
2033 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2034 } else {
2035 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2037 return;
2040 /* Dissect the information in the buffer */
2042 if (sc->proto & UMASS_PROTO_UFI) {
2043 int status;
2045 /* Section 3.4.3.1.3 specifies that the UFI command
2046 * protocol returns an ASC and ASCQ in the interrupt
2047 * data block.
2050 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2051 "ASCQ = 0x%02x\n",
2052 device_get_nameunit(sc->sc_dev),
2053 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2055 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2056 status = STATUS_CMD_OK;
2057 else
2058 status = STATUS_CMD_FAILED;
2060 sc->transfer_state = TSTATE_IDLE;
2061 sc->transfer_cb(sc, sc->transfer_priv,
2062 sc->transfer_datalen - sc->transfer_actlen,
2063 status);
2064 } else {
2065 /* Command Interrupt Data Block */
2066 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2067 device_get_nameunit(sc->sc_dev),
2068 sc->sbl.common.type, sc->sbl.common.value));
2070 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2071 int err;
2073 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2074 == IDB_VALUE_PASS) {
2075 err = STATUS_CMD_OK;
2076 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2077 == IDB_VALUE_FAIL ||
2078 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2079 == IDB_VALUE_PERSISTENT) {
2080 err = STATUS_CMD_FAILED;
2081 } else {
2082 err = STATUS_WIRE_FAILED;
2085 sc->transfer_state = TSTATE_IDLE;
2086 sc->transfer_cb(sc, sc->transfer_priv,
2087 sc->transfer_datalen-sc->transfer_actlen,
2088 err);
2091 return;
2093 case TSTATE_CBI_DCLEAR:
2094 if (err) { /* should not occur */
2095 kprintf("%s: CBI bulk-in/out stall clear failed, %s\n",
2096 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2097 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2100 sc->transfer_state = TSTATE_IDLE;
2101 sc->transfer_cb(sc, sc->transfer_priv,
2102 sc->transfer_datalen,
2103 STATUS_CMD_FAILED);
2104 return;
2106 case TSTATE_CBI_SCLEAR:
2107 if (err) /* should not occur */
2108 kprintf("%s: CBI intr-in stall clear failed, %s\n",
2109 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2111 /* Something really bad is going on. Reset the device */
2112 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2113 return;
2115 /***** CBI Reset *****/
2116 case TSTATE_CBI_RESET1:
2117 if (err)
2118 kprintf("%s: CBI reset failed, %s\n",
2119 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2121 umass_clear_endpoint_stall(sc,
2122 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2123 sc->transfer_xfer[XFER_CBI_RESET2]);
2125 return;
2126 case TSTATE_CBI_RESET2:
2127 if (err) /* should not occur */
2128 kprintf("%s: CBI bulk-in stall clear failed, %s\n",
2129 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2130 /* no error recovery, otherwise we end up in a loop */
2132 umass_clear_endpoint_stall(sc,
2133 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2134 sc->transfer_xfer[XFER_CBI_RESET3]);
2136 return;
2137 case TSTATE_CBI_RESET3:
2138 if (err) /* should not occur */
2139 kprintf("%s: CBI bulk-out stall clear failed, %s\n",
2140 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2141 /* no error recovery, otherwise we end up in a loop */
2143 sc->transfer_state = TSTATE_IDLE;
2144 if (sc->transfer_priv) {
2145 sc->transfer_cb(sc, sc->transfer_priv,
2146 sc->transfer_datalen,
2147 sc->transfer_status);
2150 return;
2153 /***** Default *****/
2154 default:
2155 panic("%s: Unknown state %d",
2156 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2164 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2167 static int
2168 umass_cam_attach_sim(struct umass_softc *sc)
2170 struct cam_devq *devq; /* Per device Queue */
2172 /* A HBA is attached to the CAM layer.
2174 * The CAM layer will then after a while start probing for
2175 * devices on the bus. The number of SIMs is limited to one.
2178 callout_init(&sc->rescan_timeout);
2179 devq = cam_simq_alloc(1 /*maximum openings*/);
2180 if (devq == NULL)
2181 return(ENOMEM);
2183 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2184 DEVNAME_SIM,
2185 sc /*priv*/,
2186 device_get_unit(sc->sc_dev) /*unit number*/,
2187 1 /*maximum device openings*/,
2188 0 /*maximum tagged device openings*/,
2189 devq);
2190 cam_simq_release(devq);
2191 if (sc->umass_sim == NULL)
2192 return(ENOMEM);
2195 * If we could not register the bus we must immediately free the
2196 * sim so we do not attempt to deregister a bus later on that we
2197 * had not registered.
2199 if (xpt_bus_register(sc->umass_sim, device_get_unit(sc->sc_dev)) !=
2200 CAM_SUCCESS) {
2201 cam_sim_free(sc->umass_sim);
2202 sc->umass_sim = NULL;
2203 return(ENOMEM);
2206 return(0);
2209 static void
2210 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2212 #ifdef USB_DEBUG
2213 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2214 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2215 periph->periph_name, periph->unit_number,
2216 ccb->ccb_h.status));
2217 } else {
2218 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2219 periph->periph_name, periph->unit_number));
2221 #endif
2223 xpt_free_path(ccb->ccb_h.path);
2224 kfree(ccb, M_USBDEV);
2227 static void
2228 umass_cam_rescan(void *addr)
2230 struct umass_softc *sc = (struct umass_softc *) addr;
2231 struct cam_path *path;
2232 union ccb *ccb;
2234 ccb = kmalloc(sizeof(union ccb), M_USBDEV, M_INTWAIT|M_ZERO);
2236 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2237 cam_sim_path(sc->umass_sim),
2238 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2239 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2241 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2242 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2243 != CAM_REQ_CMP) {
2244 kfree(ccb, M_USBDEV);
2245 return;
2248 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2249 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2250 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2251 ccb->crcn.flags = CAM_FLAG_NONE;
2252 xpt_action(ccb);
2254 /* The scan is in progress now. */
2257 static int
2258 umass_cam_attach(struct umass_softc *sc)
2260 #ifndef USB_DEBUG
2261 if (bootverbose)
2262 #endif
2263 kprintf("%s:%d:%d:%d: Attached to scbus%d\n",
2264 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2265 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2266 cam_sim_path(sc->umass_sim));
2268 if (!cold) {
2270 * Notify CAM of the new device after a 0.2 second delay. Any
2271 * failure is benign, as the user can still do it by hand
2272 * (camcontrol rescan <busno>). Only do this if we are not
2273 * booting, because CAM does a scan after booting has
2274 * completed, when interrupts have been enabled.
2276 callout_reset(&sc->rescan_timeout, MS_TO_TICKS(200),
2277 umass_cam_rescan, sc);
2280 return(0); /* always succesfull */
2283 /* umass_cam_detach
2284 * detach from the CAM layer
2287 static int
2288 umass_cam_detach_sim(struct umass_softc *sc)
2290 callout_stop(&sc->rescan_timeout);
2291 if (sc->umass_sim) {
2292 xpt_bus_deregister(cam_sim_path(sc->umass_sim));
2293 cam_sim_free(sc->umass_sim);
2295 sc->umass_sim = NULL;
2298 return(0);
2301 /* umass_cam_action
2302 * CAM requests for action come through here
2305 static void
2306 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2308 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2310 /* The softc is still there, but marked as going away. umass_cam_detach
2311 * has not yet notified CAM of the lost device however.
2313 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2314 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2315 "Invalid target (gone)\n",
2316 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2317 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2318 ccb->ccb_h.func_code));
2319 ccb->ccb_h.status = CAM_TID_INVALID;
2320 xpt_done(ccb);
2321 return;
2324 /* Verify, depending on the operation to perform, that we either got a
2325 * valid sc, because an existing target was referenced, or otherwise
2326 * the SIM is addressed.
2328 * This avoids bombing out at a kprintf and does give the CAM layer some
2329 * sensible feedback on errors.
2331 switch (ccb->ccb_h.func_code) {
2332 case XPT_SCSI_IO:
2333 case XPT_RESET_DEV:
2334 case XPT_GET_TRAN_SETTINGS:
2335 case XPT_SET_TRAN_SETTINGS:
2336 case XPT_CALC_GEOMETRY:
2337 /* the opcodes requiring a target. These should never occur. */
2338 if (sc == NULL) {
2339 kprintf("%s:%d:%d:%d:func_code 0x%04x: "
2340 "Invalid target (target needed)\n",
2341 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2342 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2343 ccb->ccb_h.func_code);
2345 ccb->ccb_h.status = CAM_TID_INVALID;
2346 xpt_done(ccb);
2347 return;
2349 break;
2350 case XPT_PATH_INQ:
2351 case XPT_NOOP:
2352 /* The opcodes sometimes aimed at a target (sc is valid),
2353 * sometimes aimed at the SIM (sc is invalid and target is
2354 * CAM_TARGET_WILDCARD)
2356 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2357 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2358 "Invalid target (no wildcard)\n",
2359 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2360 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2361 ccb->ccb_h.func_code));
2363 ccb->ccb_h.status = CAM_TID_INVALID;
2364 xpt_done(ccb);
2365 return;
2367 break;
2368 default:
2369 /* XXX Hm, we should check the input parameters */
2370 break;
2373 /* Perform the requested action */
2374 switch (ccb->ccb_h.func_code) {
2375 case XPT_SCSI_IO:
2377 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2378 int dir;
2379 unsigned char *cmd;
2380 int cmdlen;
2381 unsigned char *rcmd;
2382 int rcmdlen;
2384 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2385 "cmd: 0x%02x, flags: 0x%02x, "
2386 "%db cmd/%db data/%db sense\n",
2387 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2388 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2389 csio->cdb_io.cdb_bytes[0],
2390 ccb->ccb_h.flags & CAM_DIR_MASK,
2391 csio->cdb_len, csio->dxfer_len,
2392 csio->sense_len));
2394 /* clear the end of the buffer to make sure we don't send out
2395 * garbage.
2397 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2398 == CAM_DIR_OUT)
2399 umass_dump_buffer(sc, csio->data_ptr,
2400 csio->dxfer_len, 48));
2402 if (sc->transfer_state != TSTATE_IDLE) {
2403 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2404 "I/O in progress, deferring (state %d, %s)\n",
2405 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2406 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2407 sc->transfer_state,states[sc->transfer_state]));
2408 ccb->ccb_h.status = CAM_SCSI_BUSY;
2409 xpt_done(ccb);
2410 return;
2413 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2414 case CAM_DIR_IN:
2415 dir = DIR_IN;
2416 break;
2417 case CAM_DIR_OUT:
2418 dir = DIR_OUT;
2419 break;
2420 default:
2421 dir = DIR_NONE;
2424 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2427 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2428 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2429 } else {
2430 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2432 cmdlen = csio->cdb_len;
2433 rcmd = (unsigned char *) &sc->cam_scsi_command;
2434 rcmdlen = sizeof(sc->cam_scsi_command);
2436 /* sc->transform will convert the command to the command
2437 * (format) needed by the specific command set and return
2438 * the converted command in a buffer pointed to be rcmd.
2439 * We pass in a buffer, but if the command does not
2440 * have to be transformed it returns a ptr to the original
2441 * buffer (see umass_scsi_transform).
2444 if (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2446 * Handle EVPD inquiry for broken devices first
2447 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2449 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2450 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2451 struct scsi_sense_data *sense;
2453 sense = &ccb->csio.sense_data;
2454 bzero(sense, sizeof(*sense));
2455 sense->error_code = SSD_CURRENT_ERROR;
2456 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2457 sense->add_sense_code = 0x24;
2458 sense->extra_len = 10;
2459 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2460 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2461 CAM_AUTOSNS_VALID;
2462 xpt_done(ccb);
2463 return;
2465 /* Return fake inquiry data for broken devices */
2466 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
2467 struct ccb_scsiio *csio = &ccb->csio;
2469 memcpy(csio->data_ptr, &fake_inq_data,
2470 sizeof(fake_inq_data));
2471 csio->scsi_status = SCSI_STATUS_OK;
2472 ccb->ccb_h.status = CAM_REQ_CMP;
2473 xpt_done(ccb);
2474 return;
2476 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
2477 rcmd[0] == INQUIRY) {
2478 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
2480 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
2481 csio->data_ptr,
2482 csio->dxfer_len, dir,
2483 umass_cam_cb, (void *) ccb);
2484 } else {
2485 ccb->ccb_h.status = CAM_REQ_INVALID;
2486 xpt_done(ccb);
2489 break;
2491 case XPT_PATH_INQ:
2493 struct ccb_pathinq *cpi = &ccb->cpi;
2495 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
2496 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
2497 cam_sim_path(sc->umass_sim),
2498 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2500 /* host specific information */
2501 cpi->version_num = 1;
2502 cpi->hba_inquiry = 0;
2503 cpi->target_sprt = 0;
2504 cpi->hba_misc = PIM_NO_6_BYTE;
2505 cpi->hba_eng_cnt = 0;
2506 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2507 cpi->initiator_id = UMASS_SCSIID_HOST;
2508 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2509 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2510 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2511 cpi->unit_number = cam_sim_unit(sim);
2512 cpi->bus_id = device_get_unit(sc->sc_dev);
2514 if (sc == NULL) {
2515 cpi->base_transfer_speed = 0;
2516 cpi->max_lun = 0;
2517 } else {
2518 if (sc->quirks & FLOPPY_SPEED) {
2519 cpi->base_transfer_speed =
2520 UMASS_FLOPPY_TRANSFER_SPEED;
2521 } else if (usbd_get_speed(sc->sc_udev) ==
2522 USB_SPEED_HIGH) {
2523 cpi->base_transfer_speed =
2524 UMASS_HIGH_TRANSFER_SPEED;
2525 } else {
2526 cpi->base_transfer_speed =
2527 UMASS_FULL_TRANSFER_SPEED;
2529 cpi->max_lun = sc->maxlun;
2532 cpi->ccb_h.status = CAM_REQ_CMP;
2533 xpt_done(ccb);
2534 break;
2536 case XPT_RESET_DEV:
2538 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
2539 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2540 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2542 ccb->ccb_h.status = CAM_REQ_INPROG;
2543 umass_reset(sc, umass_cam_cb, (void *) ccb);
2544 break;
2546 case XPT_GET_TRAN_SETTINGS:
2548 struct ccb_trans_settings *cts = &ccb->cts;
2549 #ifdef CAM_NEW_TRAN_CODE
2550 cts->protocol = PROTO_SCSI;
2551 cts->protocol_version = SCSI_REV_2;
2552 cts->transport = XPORT_USB;
2553 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
2554 cts->xport_specific.valid = 0;
2556 #else
2557 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2558 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2559 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2561 cts->valid = 0;
2562 cts->flags = 0; /* no disconnection, tagging */
2563 #endif
2565 ccb->ccb_h.status = CAM_REQ_CMP;
2566 xpt_done(ccb);
2567 break;
2569 case XPT_SET_TRAN_SETTINGS:
2571 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2572 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2573 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2575 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2576 xpt_done(ccb);
2577 break;
2579 case XPT_CALC_GEOMETRY:
2581 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2582 xpt_done(ccb);
2583 break;
2585 case XPT_NOOP:
2587 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
2588 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
2589 cam_sim_path(sc->umass_sim),
2590 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2592 ccb->ccb_h.status = CAM_REQ_CMP;
2593 xpt_done(ccb);
2594 break;
2596 default:
2597 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2598 "Not implemented\n",
2599 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
2600 cam_sim_path(sc->umass_sim),
2601 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2602 ccb->ccb_h.func_code));
2604 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2605 xpt_done(ccb);
2606 break;
2610 static void
2611 umass_cam_poll(struct cam_sim *sim)
2613 struct umass_softc *sc = (struct umass_softc *) sim->softc;
2615 KKASSERT(sc != NULL);
2617 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
2618 device_get_nameunit(sc->sc_dev)));
2620 usbd_set_polling(sc->sc_udev, 1);
2621 usbd_dopoll(sc->iface);
2622 usbd_set_polling(sc->sc_udev, 0);
2626 /* umass_cam_cb
2627 * finalise a completed CAM command
2630 static void
2631 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
2633 union ccb *ccb = (union ccb *) priv;
2634 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2636 csio->resid = residue;
2638 switch (status) {
2639 case STATUS_CMD_OK:
2640 ccb->ccb_h.status = CAM_REQ_CMP;
2641 xpt_done(ccb);
2642 break;
2644 case STATUS_CMD_UNKNOWN:
2645 case STATUS_CMD_FAILED:
2646 switch (ccb->ccb_h.func_code) {
2647 case XPT_SCSI_IO:
2649 unsigned char *rcmd;
2650 int rcmdlen;
2652 /* fetch sense data */
2653 /* the rest of the command was filled in at attach */
2654 sc->cam_scsi_sense.length = csio->sense_len;
2656 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
2657 device_get_nameunit(sc->sc_dev), csio->sense_len));
2659 rcmd = (unsigned char *) &sc->cam_scsi_command;
2660 rcmdlen = sizeof(sc->cam_scsi_command);
2662 if (sc->transform(sc,
2663 (unsigned char *) &sc->cam_scsi_sense,
2664 sizeof(sc->cam_scsi_sense),
2665 &rcmd, &rcmdlen)) {
2666 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
2667 csio->sense_len = SHORT_INQUIRY_LENGTH;
2669 sc->transfer(sc, ccb->ccb_h.target_lun,
2670 rcmd, rcmdlen,
2671 &csio->sense_data,
2672 csio->sense_len, DIR_IN,
2673 umass_cam_sense_cb, (void *) ccb);
2674 } else {
2675 panic("transform(REQUEST_SENSE) failed");
2677 break;
2679 case XPT_RESET_DEV: /* Reset failed */
2680 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2681 xpt_done(ccb);
2682 break;
2683 default:
2684 panic("umass_cam_cb called for func_code %d",
2685 ccb->ccb_h.func_code);
2687 break;
2689 case STATUS_WIRE_FAILED:
2690 /* the wire protocol failed and will have recovered
2691 * (hopefully). We return an error to CAM and let CAM retry
2692 * the command if necessary.
2694 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2695 xpt_done(ccb);
2696 break;
2697 default:
2698 panic("%s: Unknown status %d in umass_cam_cb",
2699 device_get_nameunit(sc->sc_dev), status);
2703 /* Finalise a completed autosense operation
2705 static void
2706 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
2708 union ccb *ccb = (union ccb *) priv;
2709 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2710 unsigned char *rcmd;
2711 int rcmdlen;
2713 switch (status) {
2714 case STATUS_CMD_OK:
2715 case STATUS_CMD_UNKNOWN:
2716 case STATUS_CMD_FAILED:
2717 /* Getting sense data always succeeds (apart from wire
2718 * failures).
2720 if ((sc->quirks & RS_NO_CLEAR_UA)
2721 && csio->cdb_io.cdb_bytes[0] == INQUIRY
2722 && (csio->sense_data.flags & SSD_KEY)
2723 == SSD_KEY_UNIT_ATTENTION) {
2724 /* Ignore unit attention errors in the case where
2725 * the Unit Attention state is not cleared on
2726 * REQUEST SENSE. They will appear again at the next
2727 * command.
2729 ccb->ccb_h.status = CAM_REQ_CMP;
2730 } else if ((csio->sense_data.flags & SSD_KEY)
2731 == SSD_KEY_NO_SENSE) {
2732 /* No problem after all (in the case of CBI without
2733 * CCI)
2735 ccb->ccb_h.status = CAM_REQ_CMP;
2736 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
2737 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
2738 ((csio->sense_data.flags & SSD_KEY)
2739 == SSD_KEY_UNIT_ATTENTION)) {
2741 * Some devices do not clear the unit attention error
2742 * on request sense. We insert a test unit ready
2743 * command to make sure we clear the unit attention
2744 * condition, then allow the retry to proceed as
2745 * usual.
2748 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2749 | CAM_AUTOSNS_VALID;
2750 csio->scsi_status = SCSI_STATUS_CHECK_COND;
2752 #if 0
2753 DELAY(300000);
2754 #endif
2756 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
2757 "TEST_UNIT_READY\n",
2758 device_get_nameunit(sc->sc_dev)));
2760 /* the rest of the command was filled in at attach */
2762 rcmd = (unsigned char *) &sc->cam_scsi_command2;
2763 rcmdlen = sizeof(sc->cam_scsi_command2);
2765 if (sc->transform(sc,
2766 (unsigned char *)
2767 &sc->cam_scsi_test_unit_ready,
2768 sizeof(sc->cam_scsi_test_unit_ready),
2769 &rcmd, &rcmdlen)) {
2770 sc->transfer(sc, ccb->ccb_h.target_lun,
2771 rcmd, rcmdlen,
2772 NULL, 0, DIR_NONE,
2773 umass_cam_quirk_cb, (void *) ccb);
2774 } else {
2775 panic("transform(TEST_UNIT_READY) failed");
2777 break;
2778 } else {
2779 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2780 | CAM_AUTOSNS_VALID;
2781 csio->scsi_status = SCSI_STATUS_CHECK_COND;
2783 xpt_done(ccb);
2784 break;
2786 default:
2787 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
2788 device_get_nameunit(sc->sc_dev), status));
2789 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2790 xpt_done(ccb);
2795 * This completion code just handles the fact that we sent a test-unit-ready
2796 * after having previously failed a READ CAPACITY with CHECK_COND. Even
2797 * though this command succeeded, we have to tell CAM to retry.
2799 static void
2800 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
2802 union ccb *ccb = (union ccb *) priv;
2804 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
2805 device_get_nameunit(sc->sc_dev), status));
2806 #if 0
2807 ccb->ccb_h.status = CAM_REQ_CMP;
2808 #endif
2809 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2810 | CAM_AUTOSNS_VALID;
2811 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2812 xpt_done(ccb);
2815 static int
2816 umass_driver_load(module_t mod, int what, void *arg)
2818 switch (what) {
2819 case MOD_UNLOAD:
2820 case MOD_LOAD:
2821 default:
2822 return(usbd_driver_load(mod, what, arg));
2827 * SCSI specific functions
2830 static int
2831 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2832 unsigned char **rcmd, int *rcmdlen)
2834 switch (cmd[0]) {
2835 case TEST_UNIT_READY:
2836 if (sc->quirks & NO_TEST_UNIT_READY) {
2837 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
2838 ("rcmdlen = %d < %ld, buffer too small",
2839 *rcmdlen,
2840 (long)sizeof(struct scsi_start_stop_unit)));
2841 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
2842 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
2843 memset(*rcmd, 0, *rcmdlen);
2844 (*rcmd)[0] = START_STOP_UNIT;
2845 (*rcmd)[4] = SSS_START;
2846 return 1;
2848 /* fallthrough */
2849 case INQUIRY:
2850 /* some drives wedge when asked for full inquiry information. */
2851 if (sc->quirks & FORCE_SHORT_INQUIRY) {
2852 memcpy(*rcmd, cmd, cmdlen);
2853 *rcmdlen = cmdlen;
2854 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
2855 return 1;
2857 /* fallthrough */
2858 default:
2859 *rcmd = cmd; /* We don't need to copy it */
2860 *rcmdlen = cmdlen;
2863 return 1;
2865 /* RBC specific functions */
2866 static int
2867 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2868 unsigned char **rcmd, int *rcmdlen)
2870 switch (cmd[0]) {
2871 /* these commands are defined in RBC: */
2872 case READ_10:
2873 case READ_CAPACITY:
2874 case START_STOP_UNIT:
2875 case SYNCHRONIZE_CACHE:
2876 case WRITE_10:
2877 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
2878 case INQUIRY:
2879 case MODE_SELECT_10:
2880 case MODE_SENSE_10:
2881 case TEST_UNIT_READY:
2882 case WRITE_BUFFER:
2883 /* The following commands are not listed in my copy of the RBC specs.
2884 * CAM however seems to want those, and at least the Sony DSC device
2885 * appears to support those as well */
2886 case REQUEST_SENSE:
2887 case PREVENT_ALLOW:
2888 *rcmd = cmd; /* We don't need to copy it */
2889 *rcmdlen = cmdlen;
2890 return 1;
2891 /* All other commands are not legal in RBC */
2892 default:
2893 kprintf("%s: Unsupported RBC command 0x%02x",
2894 device_get_nameunit(sc->sc_dev), cmd[0]);
2895 kprintf("\n");
2896 return 0; /* failure */
2901 * UFI specific functions
2903 static int
2904 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2905 unsigned char **rcmd, int *rcmdlen)
2907 /* A UFI command is always 12 bytes in length */
2908 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
2909 ("rcmdlen = %d < %d, buffer too small",
2910 *rcmdlen, UFI_COMMAND_LENGTH));
2912 *rcmdlen = UFI_COMMAND_LENGTH;
2913 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
2915 switch (cmd[0]) {
2916 /* Commands of which the format has been verified. They should work.
2917 * Copy the command into the (zeroed out) destination buffer.
2919 case TEST_UNIT_READY:
2920 if (sc->quirks & NO_TEST_UNIT_READY) {
2921 /* Some devices do not support this command.
2922 * Start Stop Unit should give the same results
2924 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
2925 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
2926 (*rcmd)[0] = START_STOP_UNIT;
2927 (*rcmd)[4] = SSS_START;
2928 } else {
2929 memcpy(*rcmd, cmd, cmdlen);
2931 return 1;
2933 case REZERO_UNIT:
2934 case REQUEST_SENSE:
2935 case INQUIRY:
2936 case START_STOP_UNIT:
2937 case SEND_DIAGNOSTIC:
2938 case PREVENT_ALLOW:
2939 case READ_CAPACITY:
2940 case READ_10:
2941 case WRITE_10:
2942 case POSITION_TO_ELEMENT: /* SEEK_10 */
2943 case MODE_SELECT_10:
2944 case MODE_SENSE_10:
2945 case READ_12:
2946 case WRITE_12:
2947 memcpy(*rcmd, cmd, cmdlen);
2948 return 1;
2950 /* Other UFI commands: FORMAT_UNIT, READ_FORMAT_CAPACITY,
2951 * VERIFY, WRITE_AND_VERIFY.
2952 * These should be checked whether they somehow can be made to fit.
2955 default:
2956 kprintf("%s: Unsupported UFI command 0x%02x\n",
2957 device_get_nameunit(sc->sc_dev), cmd[0]);
2958 return 0; /* failure */
2963 * 8070i (ATAPI) specific functions
2965 static int
2966 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2967 unsigned char **rcmd, int *rcmdlen)
2969 /* An ATAPI command is always 12 bytes in length. */
2970 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
2971 ("rcmdlen = %d < %d, buffer too small",
2972 *rcmdlen, ATAPI_COMMAND_LENGTH));
2974 *rcmdlen = ATAPI_COMMAND_LENGTH;
2975 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
2977 switch (cmd[0]) {
2978 /* Commands of which the format has been verified. They should work.
2979 * Copy the command into the (zeroed out) destination buffer.
2981 case INQUIRY:
2982 memcpy(*rcmd, cmd, cmdlen);
2983 /* some drives wedge when asked for full inquiry information. */
2984 if (sc->quirks & FORCE_SHORT_INQUIRY)
2985 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
2986 return 1;
2988 case TEST_UNIT_READY:
2989 if (sc->quirks & NO_TEST_UNIT_READY) {
2990 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
2991 ("rcmdlen = %d < %ld, buffer too small",
2992 *rcmdlen,
2993 (long)sizeof(struct scsi_start_stop_unit)));
2994 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
2995 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
2996 memset(*rcmd, 0, *rcmdlen);
2997 (*rcmd)[0] = START_STOP_UNIT;
2998 (*rcmd)[4] = SSS_START;
2999 return 1;
3001 /* fallthrough */
3002 default:
3004 * All commands are passed through, very likely it will just work
3005 * regardless whether we know these commands or not.
3007 memcpy(*rcmd, cmd, cmdlen);
3008 return 1;
3013 /* (even the comment is missing) */
3015 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
3019 #ifdef USB_DEBUG
3020 static void
3021 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3023 int clen = cbw->bCDBLength;
3024 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3025 u_int8_t *c = cbw->CBWCDB;
3026 int tag = UGETDW(cbw->dCBWTag);
3027 int flags = cbw->bCBWFlags;
3029 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3030 "(0x%02x%02x%02x%02x%02x%02x%s), "
3031 "data = %db, dir = %s\n",
3032 device_get_nameunit(sc->sc_dev), tag, clen,
3033 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3034 dlen, (flags == CBWFLAGS_IN? "in":
3035 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3038 static void
3039 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3041 int sig = UGETDW(csw->dCSWSignature);
3042 int tag = UGETW(csw->dCSWTag);
3043 int res = UGETDW(csw->dCSWDataResidue);
3044 int status = csw->bCSWStatus;
3046 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3047 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3048 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3049 tag, res,
3050 status, (status == CSWSTATUS_GOOD? "good":
3051 (status == CSWSTATUS_FAILED? "failed":
3052 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3055 static void
3056 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3058 u_int8_t *c = cmd;
3059 int dir = sc->transfer_dir;
3061 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3062 "(0x%02x%02x%02x%02x%02x%02x%s), "
3063 "data = %db, dir = %s\n",
3064 device_get_nameunit(sc->sc_dev), cmdlen,
3065 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3066 sc->transfer_datalen,
3067 (dir == DIR_IN? "in":
3068 (dir == DIR_OUT? "out":
3069 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3072 static void
3073 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3074 int printlen)
3076 int i, j;
3077 char s1[40];
3078 char s2[40];
3079 char s3[5];
3081 s1[0] = '\0';
3082 s3[0] = '\0';
3084 ksprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3085 for (i = 0; i < buflen && i < printlen; i++) {
3086 j = i % 16;
3087 if (j == 0 && i != 0) {
3088 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3089 device_get_nameunit(sc->sc_dev), s1, s2));
3090 s2[0] = '\0';
3092 ksprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3094 if (buflen > printlen)
3095 ksprintf(s3, " ...");
3096 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3097 device_get_nameunit(sc->sc_dev), s1, s2, s3));
3099 #endif