2 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
3 * Nick Hibma <n_hibma@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
28 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
31 /* Also already merged from NetBSD:
32 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
33 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
34 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
35 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
39 * Universal Serial Bus Mass Storage Class specs:
40 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
41 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
42 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
43 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
47 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
48 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
52 * The driver handles 3 Wire Protocols
53 * - Command/Bulk/Interrupt (CBI)
54 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
55 * - Mass Storage Bulk-Only (BBB)
56 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
58 * Over these wire protocols it handles the following command protocols
60 * - UFI (floppy command set)
63 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
64 * sc->transform method is used to convert the commands into the appropriate
65 * format (if at all necessary). For example, UFI requires all commands to be
66 * 12 bytes in length amongst other things.
68 * The source code below is marked and can be split into a number of pieces
71 * - probe/attach/detach
72 * - generic transfer routines
75 * - CBI_I (in addition to functions from CBI)
76 * - CAM (Common Access Method)
81 * The protocols are implemented using a state machine, for the transfers as
82 * well as for the resets. The state machine is contained in umass_*_state.
83 * The state machine is started through either umass_*_transfer or
86 * The reason for doing this is a) CAM performs a lot better this way and b) it
87 * avoids using tsleep from interrupt context (for example after a failed
92 * The SCSI related part of this driver has been derived from the
93 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
95 * The CAM layer uses so called actions which are messages sent to the host
96 * adapter for completion. The actions come in through umass_cam_action. The
97 * appropriate block of routines is called depending on the transport protocol
98 * in use. When the transfer has finished, these routines call
99 * umass_cam_cb again to complete the CAM command.
103 * XXX Currently CBI with CCI is not supported because it bombs the system
104 * when the device is detached (low frequency interrupts are detached
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/kernel.h>
112 #include <sys/module.h>
113 #include <sys/lock.h>
114 #include <sys/mutex.h>
116 #include <sys/sysctl.h>
118 #include <dev/usb/usb.h>
119 #include <dev/usb/usbdi.h>
120 #include <dev/usb/usbdi_util.h>
124 #include <cam/cam_ccb.h>
125 #include <cam/cam_sim.h>
126 #include <cam/cam_xpt_sim.h>
127 #include <cam/scsi/scsi_all.h>
128 #include <cam/scsi/scsi_da.h>
130 #include <cam/cam_periph.h>
133 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
134 #define DPRINTF(m, x) if (umassdebug & (m)) printf x
135 #define UDMASS_GEN 0x00010000 /* general */
136 #define UDMASS_SCSI 0x00020000 /* scsi */
137 #define UDMASS_UFI 0x00040000 /* ufi command set */
138 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
139 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
140 #define UDMASS_USB 0x00100000 /* USB general */
141 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
142 #define UDMASS_CBI 0x00400000 /* CBI transfers */
143 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
144 #define UDMASS_ALL 0xffff0000 /* all of the above */
146 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
147 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
148 &umassdebug, 0, "umass debug level");
150 #define DIF(m, x) /* nop */
151 #define DPRINTF(m, x) /* nop */
155 /* Generic definitions */
157 /* Direction for umass_*_transfer */
163 #define DEVNAME "umass"
164 #define DEVNAME_SIM "umass-sim"
166 #define UMASS_MAX_TRANSFER_SIZE 65536
167 /* Approximate maximum transfer speeds (assumes 33% overhead). */
168 #define UMASS_FULL_TRANSFER_SPEED 1000
169 #define UMASS_HIGH_TRANSFER_SPEED 40000
170 #define UMASS_FLOPPY_TRANSFER_SPEED 20
172 #define UMASS_TIMEOUT 5000 /* msecs */
174 /* CAM specific definitions */
176 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
177 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
179 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
182 /* Bulk-Only features */
184 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
185 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
187 /* Command Block Wrapper */
189 uDWord dCBWSignature;
190 # define CBWSIGNATURE 0x43425355
192 uDWord dCBWDataTransferLength;
194 # define CBWFLAGS_OUT 0x00
195 # define CBWFLAGS_IN 0x80
198 # define CBWCDBLENGTH 16
199 uByte CBWCDB[CBWCDBLENGTH];
201 #define UMASS_BBB_CBW_SIZE 31
203 /* Command Status Wrapper */
205 uDWord dCSWSignature;
206 # define CSWSIGNATURE 0x53425355
207 # define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
208 # define CSWSIGNATURE_OLYMPUS_C1 0x55425355
210 uDWord dCSWDataResidue;
212 # define CSWSTATUS_GOOD 0x0
213 # define CSWSTATUS_FAILED 0x1
214 # define CSWSTATUS_PHASE 0x2
216 #define UMASS_BBB_CSW_SIZE 13
220 #define UR_CBI_ADSC 0x00
222 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
227 #define IDB_TYPE_CCI 0x00
229 #define IDB_VALUE_PASS 0x00
230 #define IDB_VALUE_FAIL 0x01
231 #define IDB_VALUE_PHASE 0x02
232 #define IDB_VALUE_PERSISTENT 0x03
233 #define IDB_VALUE_STATUS_MASK 0x03
244 struct umass_softc; /* see below */
246 typedef void (*transfer_cb_f) (struct umass_softc *sc, void *priv,
247 int residue, int status);
248 #define STATUS_CMD_OK 0 /* everything ok */
249 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
250 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
251 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
253 typedef void (*wire_reset_f) (struct umass_softc *sc, int status);
254 typedef void (*wire_transfer_f) (struct umass_softc *sc, int lun,
255 void *cmd, int cmdlen, void *data, int datalen,
256 int dir, u_int timeout, transfer_cb_f cb, void *priv);
257 typedef void (*wire_state_f) (usbd_xfer_handle xfer,
258 usbd_private_handle priv, usbd_status err);
260 typedef int (*command_transform_f) (struct umass_softc *sc,
261 unsigned char *cmd, int cmdlen,
262 unsigned char **rcmd, int *rcmdlen);
265 struct umass_devdescr_t {
267 # define VID_WILDCARD 0xffffffff
268 # define VID_EOT 0xfffffffe
270 # define PID_WILDCARD 0xffffffff
271 # define PID_EOT 0xfffffffe
273 # define RID_WILDCARD 0xffffffff
274 # define RID_EOT 0xfffffffe
276 /* wire and command protocol */
278 # define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
279 # define UMASS_PROTO_CBI 0x0002
280 # define UMASS_PROTO_CBI_I 0x0004
281 # define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
282 # define UMASS_PROTO_SCSI 0x0100 /* command protocol */
283 # define UMASS_PROTO_ATAPI 0x0200
284 # define UMASS_PROTO_UFI 0x0400
285 # define UMASS_PROTO_RBC 0x0800
286 # define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
288 /* Device specific quirks */
290 # define NO_QUIRKS 0x0000
291 /* The drive does not support Test Unit Ready. Convert to Start Unit
293 # define NO_TEST_UNIT_READY 0x0001
294 /* The drive does not reset the Unit Attention state after REQUEST
295 * SENSE has been sent. The INQUIRY command does not reset the UA
296 * either, and so CAM runs in circles trying to retrieve the initial
299 # define RS_NO_CLEAR_UA 0x0002
300 /* The drive does not support START STOP. */
301 # define NO_START_STOP 0x0004
302 /* Don't ask for full inquiry data (255b). */
303 # define FORCE_SHORT_INQUIRY 0x0008
304 /* Needs to be initialised the Shuttle way */
305 # define SHUTTLE_INIT 0x0010
306 /* Drive needs to be switched to alternate iface 1 */
307 # define ALT_IFACE_1 0x0020
308 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
309 # define FLOPPY_SPEED 0x0040
310 /* The device can't count and gets the residue of transfers wrong */
311 # define IGNORE_RESIDUE 0x0080
312 /* No GetMaxLun call */
313 # define NO_GETMAXLUN 0x0100
314 /* The device uses a weird CSWSIGNATURE. */
315 # define WRONG_CSWSIG 0x0200
316 /* Device cannot handle INQUIRY so fake a generic response */
317 # define NO_INQUIRY 0x0400
318 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
319 # define NO_INQUIRY_EVPD 0x0800
320 /* Pad all RBC requests to 12 bytes. */
321 # define RBC_PAD_TO_12 0x1000
322 /* Device reports number of sectors from READ_CAPACITY, not max
325 # define READ_CAPACITY_OFFBY1 0x2000
326 /* Device cannot handle a SCSI synchronize cache command. Normally
327 * this quirk would be handled in the cam layer, but for IDE bridges
328 * we need to associate the quirk with the bridge and not the
329 * underlying disk device. This is handled by faking a success result.
331 # define NO_SYNCHRONIZE_CACHE 0x4000
334 static struct umass_devdescr_t umass_devdescrs[] = {
335 { USB_VENDOR_ADDONICS2, USB_PRODUCT_ADDONICS2_CABLE_205, RID_WILDCARD,
336 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
339 { USB_VENDOR_AIPTEK, USB_PRODUCT_AIPTEK_POCKETCAM3M, RID_WILDCARD,
340 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
343 { USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO230, RID_WILDCARD,
344 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
347 { USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO330, RID_WILDCARD,
348 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
351 { USB_VENDOR_ASAHIOPTICAL, PID_WILDCARD, RID_WILDCARD,
352 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
355 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_ATTACHE, RID_WILDCARD,
356 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
359 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_A256MB, RID_WILDCARD,
360 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
363 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_DISKPRO512, RID_WILDCARD,
364 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
367 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2SCSI, RID_WILDCARD,
368 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
371 { USB_VENDOR_CASIO, USB_PRODUCT_CASIO_QV_DIGICAM, RID_WILDCARD,
372 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
375 { USB_VENDOR_CCYU, USB_PRODUCT_CCYU_ED1064, RID_WILDCARD,
376 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
379 { USB_VENDOR_CENTURY, USB_PRODUCT_CENTURY_EX35QUAT, RID_WILDCARD,
380 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
381 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
383 { USB_VENDOR_DESKNOTE, USB_PRODUCT_DESKNOTE_UCR_61S2B, RID_WILDCARD,
384 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
387 { USB_VENDOR_DMI, USB_PRODUCT_DMI_CFSM_RW, RID_WILDCARD,
391 { USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_875DC, RID_WILDCARD,
392 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
395 { USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_895, RID_WILDCARD,
396 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
399 { USB_VENDOR_FEIYA, USB_PRODUCT_FEIYA_5IN1, RID_WILDCARD,
400 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
403 { USB_VENDOR_FREECOM, USB_PRODUCT_FREECOM_DVD, RID_WILDCARD,
407 { USB_VENDOR_FUJIPHOTO, USB_PRODUCT_FUJIPHOTO_MASS0100, RID_WILDCARD,
408 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
411 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE, RID_WILDCARD,
412 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
413 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
415 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE_2, RID_WILDCARD,
416 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
417 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
419 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB, RID_WILDCARD,
420 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
421 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
423 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB_2, RID_WILDCARD,
424 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
427 { USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FG, RID_WILDCARD,
428 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
431 { USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FGSM, RID_WILDCARD,
432 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
435 { USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_USB, RID_WILDCARD,
436 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
439 { USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_DZ_MV100A, RID_WILDCARD,
440 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
443 { USB_VENDOR_HP, USB_PRODUCT_HP_CDW4E, RID_WILDCARD,
447 { USB_VENDOR_HP, USB_PRODUCT_HP_CDW8200, RID_WILDCARD,
448 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
449 NO_TEST_UNIT_READY | NO_START_STOP
451 { USB_VENDOR_IMAGINATION, USB_PRODUCT_IMAGINATION_DBX1, RID_WILDCARD,
452 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
455 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_ATAPI, RID_WILDCARD,
456 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
459 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_STORAGE_V2, RID_WILDCARD,
460 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
463 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_USBCABLE, RID_WILDCARD,
464 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
465 NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
467 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_IU_CD2, RID_WILDCARD,
468 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
471 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_DVR_UEH8, RID_WILDCARD,
472 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
475 { USB_VENDOR_IOMEGA, USB_PRODUCT_IOMEGA_ZIP100, RID_WILDCARD,
476 /* XXX This is not correct as there are Zip drives that use ATAPI.
478 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
481 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_L3, RID_WILDCARD,
482 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
485 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S3X, RID_WILDCARD,
486 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
489 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S4, RID_WILDCARD,
490 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
493 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S5, RID_WILDCARD,
494 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
497 { USB_VENDOR_LACIE, USB_PRODUCT_LACIE_HD, RID_WILDCARD,
498 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
501 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_CF_READER, RID_WILDCARD,
502 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
505 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_JUMPSHOT, RID_WILDCARD,
509 { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443SU2, RID_WILDCARD,
513 { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443U2, RID_WILDCARD,
514 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
517 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_DUBPXXG, RID_WILDCARD,
518 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
519 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
521 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_DPCM, RID_WILDCARD,
522 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
523 NO_TEST_UNIT_READY | NO_START_STOP
525 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIDB25, RID_WILDCARD,
526 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
529 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIHD50, RID_WILDCARD,
530 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
533 { USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_E223, RID_WILDCARD,
537 { USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_F300, RID_WILDCARD,
538 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
541 { USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_CDRRW, RID_WILDCARD,
542 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
545 { USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_FDD, RID_WILDCARD,
546 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
549 { USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_E398, RID_WILDCARD,
550 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
551 FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
553 { USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY, RID_WILDCARD,
554 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
555 IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
557 { USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY2, RID_WILDCARD,
558 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
561 { USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN, RID_WILDCARD,
562 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
563 NO_INQUIRY | IGNORE_RESIDUE
565 { USB_VENDOR_NEODIO, USB_PRODUCT_NEODIO_ND3260, RID_WILDCARD,
566 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
569 { USB_VENDOR_NETAC, USB_PRODUCT_NETAC_CF_CARD, RID_WILDCARD,
570 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
573 { USB_VENDOR_NETAC, USB_PRODUCT_NETAC_ONLYDISK, RID_WILDCARD,
574 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
577 { USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_CLIK_40, RID_WILDCARD,
581 { USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C1, RID_WILDCARD,
582 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
585 { USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C700, RID_WILDCARD,
589 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFMS_RW, RID_WILDCARD,
593 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_COMBO, RID_WILDCARD,
597 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER, RID_WILDCARD,
601 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER2, RID_WILDCARD,
605 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDCFE_B_CF_READER, RID_WILDCARD,
609 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDSM_B_READER, RID_WILDCARD,
613 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_READER, RID_WILDCARD,
617 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_UCF100, RID_WILDCARD,
618 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
619 NO_INQUIRY | NO_GETMAXLUN
621 { USB_VENDOR_ONSPEC2, USB_PRODUCT_ONSPEC2_IMAGEMATE_SDDR55, RID_WILDCARD,
625 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXL840AN, RID_WILDCARD,
626 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
629 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB20AN, RID_WILDCARD,
630 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
633 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB35AN, RID_WILDCARD,
634 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
637 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_LS120CAM, RID_WILDCARD,
641 { USB_VENDOR_PLEXTOR, USB_PRODUCT_PLEXTOR_40_12_40U, RID_WILDCARD,
642 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
645 { USB_VENDOR_PNY, USB_PRODUCT_PNY_ATTACHE2, RID_WILDCARD,
646 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
647 IGNORE_RESIDUE | NO_START_STOP
649 { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_YP_U2, RID_WILDCARD,
650 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
651 SHUTTLE_INIT | NO_GETMAXLUN
653 { USB_VENDOR_SAMSUNG_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
654 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
657 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
658 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
659 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
661 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
663 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
665 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
666 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
667 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
669 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
670 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
673 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
674 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
677 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
678 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
681 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
682 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
685 { USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
686 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
689 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
690 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
693 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
694 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
697 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
698 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
699 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
701 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
702 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
705 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
709 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
710 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
713 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
714 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
717 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
721 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
722 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
725 { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
726 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
729 { USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
730 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
733 { USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
734 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737 { USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
738 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
741 { USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
742 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
745 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
746 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
749 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
750 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
753 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
754 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
757 { USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
758 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
761 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
762 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
765 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
766 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
769 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
770 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
773 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
774 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
777 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
778 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
781 { USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
782 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
785 { USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
789 { USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
790 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
793 { USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
794 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
797 { USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
798 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
801 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
802 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
805 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
809 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
813 { USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
814 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
817 { USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
818 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
821 { USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
822 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
825 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
826 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
827 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
829 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, RID_WILDCARD,
830 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
831 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
833 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYBOOK, RID_WILDCARD,
834 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
837 { USB_VENDOR_WINMAXGROUP, USB_PRODUCT_WINMAXGROUP_FLASH64MC, RID_WILDCARD,
838 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
841 { USB_VENDOR_YANO, USB_PRODUCT_YANO_FW800HD, RID_WILDCARD,
842 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
843 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
845 { USB_VENDOR_YANO, USB_PRODUCT_YANO_U640MO, RID_WILDCARD,
846 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
849 { USB_VENDOR_YEDATA, USB_PRODUCT_YEDATA_FLASHBUSTERU, RID_WILDCARD,
850 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
853 { USB_VENDOR_ZORAN, USB_PRODUCT_ZORAN_EX20DSC, RID_WILDCARD,
854 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
857 { VID_EOT, PID_EOT, RID_EOT, 0, 0 }
861 /* the per device structure */
863 device_t sc_dev; /* base device */
864 usbd_device_handle sc_udev; /* USB device */
866 struct cam_sim *umass_sim; /* SCSI Interface Module */
868 unsigned char flags; /* various device flags */
869 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
871 u_int16_t proto; /* wire and cmd protocol */
872 u_int16_t quirks; /* they got it almost right */
874 usbd_interface_handle iface; /* Mass Storage interface */
875 int ifaceno; /* MS iface number */
877 u_int8_t bulkin; /* bulk-in Endpoint Address */
878 u_int8_t bulkout; /* bulk-out Endpoint Address */
879 u_int8_t intrin; /* intr-in Endp. (CBI) */
880 usbd_pipe_handle bulkin_pipe;
881 usbd_pipe_handle bulkout_pipe;
882 usbd_pipe_handle intrin_pipe;
884 /* Reset the device in a wire protocol specific way */
887 /* The start of a wire transfer. It prepares the whole transfer (cmd,
888 * data, and status stage) and initiates it. It is up to the state
889 * machine (below) to handle the various stages and errors in these
891 wire_transfer_f transfer;
893 /* The state machine, handling the various states during a transfer */
896 /* The command transform function is used to conver the SCSI commands
897 * into their derivatives, like UFI, ATAPI, and friends.
899 command_transform_f transform; /* command transform */
901 /* Bulk specific variables for transfers in progress */
902 umass_bbb_cbw_t cbw; /* command block wrapper */
903 umass_bbb_csw_t csw; /* command status wrapper*/
904 /* CBI specific variables for transfers in progress */
905 umass_cbi_cbl_t cbl; /* command block */
906 umass_cbi_sbl_t sbl; /* status block */
908 /* generic variables for transfers in progress */
909 /* ctrl transfer requests */
910 usb_device_request_t request;
913 * Most of our operations are initiated from interrupt context, so
914 * we need to avoid using the one that is in use. We want to avoid
915 * allocating them in the interrupt context as well.
917 /* indices into array below */
918 # define XFER_BBB_CBW 0 /* Bulk-Only */
919 # define XFER_BBB_DATA 1
920 # define XFER_BBB_DCLEAR 2
921 # define XFER_BBB_CSW1 3
922 # define XFER_BBB_CSW2 4
923 # define XFER_BBB_SCLEAR 5
924 # define XFER_BBB_RESET1 6
925 # define XFER_BBB_RESET2 7
926 # define XFER_BBB_RESET3 8
928 # define XFER_CBI_CB 0 /* CBI */
929 # define XFER_CBI_DATA 1
930 # define XFER_CBI_STATUS 2
931 # define XFER_CBI_DCLEAR 3
932 # define XFER_CBI_SCLEAR 4
933 # define XFER_CBI_RESET1 5
934 # define XFER_CBI_RESET2 6
935 # define XFER_CBI_RESET3 7
937 # define XFER_NR 9 /* maximum number */
939 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
941 int transfer_dir; /* data direction */
942 void *transfer_data; /* data buffer */
943 int transfer_datalen; /* (maximum) length */
944 int transfer_actlen; /* actual length */
945 transfer_cb_f transfer_cb; /* callback */
946 void *transfer_priv; /* for callback */
950 # define TSTATE_ATTACH 0 /* in attach */
951 # define TSTATE_IDLE 1
952 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
953 # define TSTATE_BBB_DATA 3 /* Data transfer */
954 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
955 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
956 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
957 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
958 # define TSTATE_BBB_RESET1 8 /* reset command */
959 # define TSTATE_BBB_RESET2 9 /* in clear stall */
960 # define TSTATE_BBB_RESET3 10 /* out clear stall */
961 # define TSTATE_CBI_COMMAND 11 /* command transfer */
962 # define TSTATE_CBI_DATA 12 /* data transfer */
963 # define TSTATE_CBI_STATUS 13 /* status transfer */
964 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
965 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
966 # define TSTATE_CBI_RESET1 16 /* reset command */
967 # define TSTATE_CBI_RESET2 17 /* in clear stall */
968 # define TSTATE_CBI_RESET3 18 /* out clear stall */
969 # define TSTATE_STATES 19 /* # of states above */
972 /* SCSI/CAM specific variables */
973 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
974 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
975 struct scsi_sense cam_scsi_sense;
976 struct scsi_sense cam_scsi_test_unit_ready;
977 struct callout cam_scsi_rescan_ch;
979 int timeout; /* in msecs */
981 int maxlun; /* maximum LUN number */
985 char *states[TSTATE_STATES+1] = {
986 /* should be kept in sync with the list at transfer_state */
991 "BBB Data bulk-in/-out clear stall",
992 "BBB CSW, 1st attempt",
993 "BBB CSW bulk-in clear stall",
994 "BBB CSW, 2nd attempt",
996 "BBB bulk-in clear stall",
997 "BBB bulk-out clear stall",
1001 "CBI Data bulk-in/-out clear stall",
1002 "CBI Status intr-in clear stall",
1004 "CBI bulk-in clear stall",
1005 "CBI bulk-out clear stall",
1010 /* If device cannot return valid inquiry data, fake it */
1011 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1012 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
1013 /*additional_length*/ 31, 0, 0, 0
1016 /* USB device probe/attach/detach functions */
1017 static device_probe_t umass_match;
1018 static device_attach_t umass_attach;
1019 static device_detach_t umass_detach;
1021 static device_method_t umass_methods[] = {
1022 /* Device interface */
1023 DEVMETHOD(device_probe, umass_match),
1024 DEVMETHOD(device_attach, umass_attach),
1025 DEVMETHOD(device_detach, umass_detach),
1030 static driver_t umass_driver = {
1033 sizeof(struct umass_softc)
1036 static devclass_t umass_devclass;
1038 static int umass_match_proto (struct umass_softc *sc,
1039 usbd_interface_handle iface,
1040 usbd_device_handle udev);
1042 /* quirk functions */
1043 static void umass_init_shuttle (struct umass_softc *sc);
1045 /* generic transfer functions */
1046 static usbd_status umass_setup_transfer (struct umass_softc *sc,
1047 usbd_pipe_handle pipe,
1048 void *buffer, int buflen, int flags,
1049 usbd_xfer_handle xfer);
1050 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
1051 usbd_device_handle udev,
1052 usb_device_request_t *req,
1053 void *buffer, int buflen, int flags,
1054 usbd_xfer_handle xfer);
1055 static void umass_clear_endpoint_stall (struct umass_softc *sc,
1056 u_int8_t endpt, usbd_pipe_handle pipe,
1057 int state, usbd_xfer_handle xfer);
1058 static void umass_reset (struct umass_softc *sc,
1059 transfer_cb_f cb, void *priv);
1061 /* Bulk-Only related functions */
1062 static void umass_bbb_reset (struct umass_softc *sc, int status);
1063 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
1064 void *cmd, int cmdlen,
1065 void *data, int datalen, int dir, u_int timeout,
1066 transfer_cb_f cb, void *priv);
1067 static void umass_bbb_state (usbd_xfer_handle xfer,
1068 usbd_private_handle priv,
1070 static int umass_bbb_get_max_lun
1071 (struct umass_softc *sc);
1073 /* CBI related functions */
1074 static int umass_cbi_adsc (struct umass_softc *sc,
1075 char *buffer, int buflen,
1076 usbd_xfer_handle xfer);
1077 static void umass_cbi_reset (struct umass_softc *sc, int status);
1078 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
1079 void *cmd, int cmdlen,
1080 void *data, int datalen, int dir, u_int timeout,
1081 transfer_cb_f cb, void *priv);
1082 static void umass_cbi_state (usbd_xfer_handle xfer,
1083 usbd_private_handle priv, usbd_status err);
1085 /* CAM related functions */
1086 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
1087 static void umass_cam_poll (struct cam_sim *sim);
1089 static void umass_cam_cb (struct umass_softc *sc, void *priv,
1090 int residue, int status);
1091 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
1092 int residue, int status);
1093 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
1094 int residue, int status);
1096 static void umass_cam_rescan_callback
1097 (struct cam_periph *periph,union ccb *ccb);
1098 static void umass_cam_rescan (void *addr);
1100 static int umass_cam_attach_sim (struct umass_softc *sc);
1101 static int umass_cam_attach (struct umass_softc *sc);
1102 static int umass_cam_detach_sim (struct umass_softc *sc);
1105 /* SCSI specific functions */
1106 static int umass_scsi_transform (struct umass_softc *sc,
1107 unsigned char *cmd, int cmdlen,
1108 unsigned char **rcmd, int *rcmdlen);
1110 /* UFI specific functions */
1111 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1112 static int umass_ufi_transform (struct umass_softc *sc,
1113 unsigned char *cmd, int cmdlen,
1114 unsigned char **rcmd, int *rcmdlen);
1116 /* ATAPI (8070i) specific functions */
1117 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1118 static int umass_atapi_transform (struct umass_softc *sc,
1119 unsigned char *cmd, int cmdlen,
1120 unsigned char **rcmd, int *rcmdlen);
1122 /* RBC specific functions */
1123 static int umass_rbc_transform (struct umass_softc *sc,
1124 unsigned char *cmd, int cmdlen,
1125 unsigned char **rcmd, int *rcmdlen);
1128 /* General debugging functions */
1129 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
1130 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
1131 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
1132 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
1133 int buflen, int printlen);
1136 MODULE_DEPEND(umass, cam, 1, 1, 1);
1137 MODULE_DEPEND(umass, usb, 1, 1, 1);
1140 * USB device probe/attach/detach
1144 * Match the device we are seeing with the devices supported. Fill in the
1145 * description in the softc accordingly. This function is called from both
1150 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
1151 usbd_device_handle udev)
1153 usb_device_descriptor_t *dd;
1154 usb_interface_descriptor_t *id;
1162 dd = usbd_get_device_descriptor(udev);
1164 /* An entry specifically for Y-E Data devices as they don't fit in the
1165 * device description table.
1167 if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA
1168 && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) {
1170 /* Revisions < 1.28 do not handle the interrupt endpoint
1173 if (UGETW(dd->bcdDevice) < 0x128) {
1174 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1176 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1180 * Revisions < 1.28 do not have the TEST UNIT READY command
1181 * Revisions == 1.28 have a broken TEST UNIT READY
1183 if (UGETW(dd->bcdDevice) <= 0x128)
1184 sc->quirks |= NO_TEST_UNIT_READY;
1186 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1187 return(UMATCH_VENDOR_PRODUCT);
1190 /* Check the list of supported devices for a match. While looking,
1191 * check for wildcarded and fully matched. First match wins.
1193 for (i = 0; umass_devdescrs[i].vid != VID_EOT && !found; i++) {
1194 if (umass_devdescrs[i].vid == VID_WILDCARD &&
1195 umass_devdescrs[i].pid == PID_WILDCARD &&
1196 umass_devdescrs[i].rid == RID_WILDCARD) {
1197 printf("umass: ignoring invalid wildcard quirk\n");
1200 if ((umass_devdescrs[i].vid == UGETW(dd->idVendor) ||
1201 umass_devdescrs[i].vid == VID_WILDCARD)
1202 && (umass_devdescrs[i].pid == UGETW(dd->idProduct) ||
1203 umass_devdescrs[i].pid == PID_WILDCARD)) {
1204 if (umass_devdescrs[i].rid == RID_WILDCARD) {
1205 sc->proto = umass_devdescrs[i].proto;
1206 sc->quirks = umass_devdescrs[i].quirks;
1207 return (UMATCH_VENDOR_PRODUCT);
1208 } else if (umass_devdescrs[i].rid ==
1209 UGETW(dd->bcdDevice)) {
1210 sc->proto = umass_devdescrs[i].proto;
1211 sc->quirks = umass_devdescrs[i].quirks;
1212 return (UMATCH_VENDOR_PRODUCT_REV);
1213 } /* else RID does not match */
1217 /* Check for a standards compliant device */
1218 id = usbd_get_interface_descriptor(iface);
1219 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
1220 return(UMATCH_NONE);
1222 switch (id->bInterfaceSubClass) {
1223 case UISUBCLASS_SCSI:
1224 sc->proto |= UMASS_PROTO_SCSI;
1226 case UISUBCLASS_UFI:
1227 sc->proto |= UMASS_PROTO_UFI;
1229 case UISUBCLASS_RBC:
1230 sc->proto |= UMASS_PROTO_RBC;
1232 case UISUBCLASS_SFF8020I:
1233 case UISUBCLASS_SFF8070I:
1234 sc->proto |= UMASS_PROTO_ATAPI;
1237 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
1238 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
1239 return(UMATCH_NONE);
1242 switch (id->bInterfaceProtocol) {
1243 case UIPROTO_MASS_CBI:
1244 sc->proto |= UMASS_PROTO_CBI;
1246 case UIPROTO_MASS_CBI_I:
1247 sc->proto |= UMASS_PROTO_CBI_I;
1249 case UIPROTO_MASS_BBB_OLD:
1250 case UIPROTO_MASS_BBB:
1251 sc->proto |= UMASS_PROTO_BBB;
1254 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
1255 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
1256 return(UMATCH_NONE);
1259 return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
1263 umass_match(device_t self)
1265 struct usb_attach_arg *uaa = device_get_ivars(self);
1266 struct umass_softc *sc = device_get_softc(self);
1269 if (uaa->iface == NULL)
1270 return(UMATCH_NONE);
1271 return(umass_match_proto(sc, uaa->iface, uaa->device));
1275 umass_attach(device_t self)
1277 struct umass_softc *sc = device_get_softc(self);
1278 struct usb_attach_arg *uaa = device_get_ivars(self);
1279 usb_interface_descriptor_t *id;
1280 usb_endpoint_descriptor_t *ed;
1285 * the softc struct is bzero-ed in device_set_driver. We can safely
1286 * call umass_detach without specifically initialising the struct.
1289 sc->iface = uaa->iface;
1290 sc->ifaceno = uaa->ifaceno;
1291 callout_init(&sc->cam_scsi_rescan_ch, 0);
1293 /* initialise the proto and drive values in the umass_softc (again) */
1294 (void) umass_match_proto(sc, sc->iface, uaa->device);
1296 id = usbd_get_interface_descriptor(sc->iface);
1298 printf("%s: ", device_get_nameunit(sc->sc_dev));
1299 switch (sc->proto&UMASS_PROTO_COMMAND) {
1300 case UMASS_PROTO_SCSI:
1303 case UMASS_PROTO_ATAPI:
1304 printf("8070i (ATAPI)");
1306 case UMASS_PROTO_UFI:
1309 case UMASS_PROTO_RBC:
1313 printf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
1317 switch (sc->proto&UMASS_PROTO_WIRE) {
1318 case UMASS_PROTO_BBB:
1319 printf("Bulk-Only");
1321 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1324 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1325 printf("CBI with CCI");
1327 printf(" (using CBI)");
1331 printf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
1333 printf("; quirks = 0x%04x\n", sc->quirks);
1337 if (sc->proto & UMASS_PROTO_CBI_I) {
1338 /* See beginning of file for comment on the use of CBI with CCI */
1339 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
1343 if (sc->quirks & ALT_IFACE_1) {
1344 err = usbd_set_interface(uaa->iface, 1);
1346 DPRINTF(UDMASS_USB, ("%s: could not switch to "
1347 "Alt Interface %d\n",
1348 device_get_nameunit(sc->sc_dev), 1));
1355 * In addition to the Control endpoint the following endpoints
1357 * a) bulk-in endpoint.
1358 * b) bulk-out endpoint.
1359 * and for Control/Bulk/Interrupt with CCI (CBI_I)
1362 * The endpoint addresses are not fixed, so we have to read them
1363 * from the device descriptors of the current interface.
1365 for (i = 0 ; i < id->bNumEndpoints ; i++) {
1366 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
1368 printf("%s: could not read endpoint descriptor\n",
1369 device_get_nameunit(sc->sc_dev));
1372 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1373 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1374 sc->bulkin = ed->bEndpointAddress;
1375 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
1376 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1377 sc->bulkout = ed->bEndpointAddress;
1378 } else if (sc->proto & UMASS_PROTO_CBI_I
1379 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1380 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1381 sc->intrin = ed->bEndpointAddress;
1383 if (UGETW(ed->wMaxPacketSize) > 2) {
1384 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
1385 device_get_nameunit(sc->sc_dev),
1386 UGETW(ed->wMaxPacketSize)));
1392 /* check whether we found all the endpoints we need */
1393 if (!sc->bulkin || !sc->bulkout
1394 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
1395 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
1396 device_get_nameunit(sc->sc_dev),
1397 sc->bulkin, sc->bulkout, sc->intrin));
1402 /* Open the bulk-in and -out pipe */
1403 err = usbd_open_pipe(sc->iface, sc->bulkout,
1404 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
1406 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
1407 device_get_nameunit(sc->sc_dev), sc->bulkout));
1411 err = usbd_open_pipe(sc->iface, sc->bulkin,
1412 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
1414 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
1415 device_get_nameunit(sc->sc_dev), sc->bulkin));
1419 /* Open the intr-in pipe if the protocol is CBI with CCI.
1420 * Note: early versions of the Zip drive do have an interrupt pipe, but
1421 * this pipe is unused.
1423 * We do not open the interrupt pipe as an interrupt pipe, but as a
1424 * normal bulk endpoint. We send an IN transfer down the wire at the
1425 * appropriate time, because we know exactly when to expect data on
1426 * that endpoint. This saves bandwidth, but more important, makes the
1427 * code for handling the data on that endpoint simpler. No data
1428 * arriving concurrently.
1430 if (sc->proto & UMASS_PROTO_CBI_I) {
1431 err = usbd_open_pipe(sc->iface, sc->intrin,
1432 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1434 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1435 device_get_nameunit(sc->sc_dev), sc->intrin));
1441 /* initialisation of generic part */
1442 sc->transfer_state = TSTATE_ATTACH;
1444 /* request a sufficient number of xfer handles */
1445 for (i = 0; i < XFER_NR; i++) {
1446 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1447 if (!sc->transfer_xfer[i]) {
1448 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1449 device_get_nameunit(sc->sc_dev)));
1455 /* Initialise the wire protocol specific methods */
1456 if (sc->proto & UMASS_PROTO_BBB) {
1457 sc->reset = umass_bbb_reset;
1458 sc->transfer = umass_bbb_transfer;
1459 sc->state = umass_bbb_state;
1460 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1461 sc->reset = umass_cbi_reset;
1462 sc->transfer = umass_cbi_transfer;
1463 sc->state = umass_cbi_state;
1466 panic("%s:%d: Unknown proto 0x%02x",
1467 __FILE__, __LINE__, sc->proto);
1471 if (sc->proto & UMASS_PROTO_SCSI)
1472 sc->transform = umass_scsi_transform;
1473 else if (sc->proto & UMASS_PROTO_UFI)
1474 sc->transform = umass_ufi_transform;
1475 else if (sc->proto & UMASS_PROTO_ATAPI)
1476 sc->transform = umass_atapi_transform;
1477 else if (sc->proto & UMASS_PROTO_RBC)
1478 sc->transform = umass_rbc_transform;
1481 panic("No transformation defined for command proto 0x%02x",
1482 sc->proto & UMASS_PROTO_COMMAND);
1485 /* From here onwards the device can be used. */
1487 if (sc->quirks & SHUTTLE_INIT)
1488 umass_init_shuttle(sc);
1490 /* Get the maximum LUN supported by the device.
1492 if (((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1493 !(sc->quirks & NO_GETMAXLUN))
1494 sc->maxlun = umass_bbb_get_max_lun(sc);
1498 if ((sc->proto & UMASS_PROTO_SCSI) ||
1499 (sc->proto & UMASS_PROTO_ATAPI) ||
1500 (sc->proto & UMASS_PROTO_UFI) ||
1501 (sc->proto & UMASS_PROTO_RBC)) {
1502 /* Prepare the SCSI command block */
1503 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1504 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1506 /* register the SIM */
1507 err = umass_cam_attach_sim(sc);
1512 /* scan the new sim */
1513 err = umass_cam_attach(sc);
1515 umass_cam_detach_sim(sc);
1520 panic("%s:%d: Unknown proto 0x%02x",
1521 __FILE__, __LINE__, sc->proto);
1524 sc->transfer_state = TSTATE_IDLE;
1525 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1531 umass_detach(device_t self)
1533 struct umass_softc *sc = device_get_softc(self);
1537 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1539 sc->flags |= UMASS_FLAGS_GONE;
1541 /* abort all the pipes in case there are transfers active. */
1542 usbd_abort_default_pipe(sc->sc_udev);
1543 if (sc->bulkout_pipe)
1544 usbd_abort_pipe(sc->bulkout_pipe);
1545 if (sc->bulkin_pipe)
1546 usbd_abort_pipe(sc->bulkin_pipe);
1547 if (sc->intrin_pipe)
1548 usbd_abort_pipe(sc->intrin_pipe);
1550 callout_drain(&sc->cam_scsi_rescan_ch);
1551 if ((sc->proto & UMASS_PROTO_SCSI) ||
1552 (sc->proto & UMASS_PROTO_ATAPI) ||
1553 (sc->proto & UMASS_PROTO_UFI) ||
1554 (sc->proto & UMASS_PROTO_RBC))
1555 /* detach the SCSI host controller (SIM) */
1556 err = umass_cam_detach_sim(sc);
1558 for (i = 0; i < XFER_NR; i++)
1559 if (sc->transfer_xfer[i])
1560 usbd_free_xfer(sc->transfer_xfer[i]);
1562 /* remove all the pipes */
1563 if (sc->bulkout_pipe)
1564 usbd_close_pipe(sc->bulkout_pipe);
1565 if (sc->bulkin_pipe)
1566 usbd_close_pipe(sc->bulkin_pipe);
1567 if (sc->intrin_pipe)
1568 usbd_close_pipe(sc->intrin_pipe);
1574 umass_init_shuttle(struct umass_softc *sc)
1576 usb_device_request_t req;
1579 /* The Linux driver does this, but no one can tell us what the
1582 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1583 req.bRequest = 1; /* XXX unknown command */
1584 USETW(req.wValue, 0);
1585 USETW(req.wIndex, sc->ifaceno);
1586 USETW(req.wLength, sizeof status);
1587 (void) usbd_do_request(sc->sc_udev, &req, &status);
1589 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1590 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1594 * Generic functions to handle transfers
1598 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1599 void *buffer, int buflen, int flags,
1600 usbd_xfer_handle xfer)
1604 /* Initialise a USB transfer and then schedule it */
1606 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1607 sc->timeout, sc->state);
1609 err = usbd_transfer(xfer);
1610 if (err && err != USBD_IN_PROGRESS) {
1611 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1612 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1616 return (USBD_NORMAL_COMPLETION);
1621 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1622 usb_device_request_t *req,
1623 void *buffer, int buflen, int flags,
1624 usbd_xfer_handle xfer)
1628 /* Initialise a USB control transfer and then schedule it */
1630 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1631 sc->timeout, req, buffer, buflen, flags, sc->state);
1633 err = usbd_transfer(xfer);
1634 if (err && err != USBD_IN_PROGRESS) {
1635 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1636 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1638 /* do not reset, as this would make us loop */
1642 return (USBD_NORMAL_COMPLETION);
1646 umass_clear_endpoint_stall(struct umass_softc *sc,
1647 u_int8_t endpt, usbd_pipe_handle pipe,
1648 int state, usbd_xfer_handle xfer)
1650 usbd_device_handle udev;
1652 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1653 device_get_nameunit(sc->sc_dev), endpt));
1655 usbd_interface2device_handle(sc->iface, &udev);
1657 sc->transfer_state = state;
1659 usbd_clear_endpoint_toggle(pipe);
1661 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1662 sc->request.bRequest = UR_CLEAR_FEATURE;
1663 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1664 USETW(sc->request.wIndex, endpt);
1665 USETW(sc->request.wLength, 0);
1666 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1670 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1672 sc->transfer_cb = cb;
1673 sc->transfer_priv = priv;
1675 /* The reset is a forced reset, so no error (yet) */
1676 sc->reset(sc, STATUS_CMD_OK);
1680 * Bulk protocol specific functions
1684 umass_bbb_reset(struct umass_softc *sc, int status)
1686 usbd_device_handle udev;
1688 KASSERT(sc->proto & UMASS_PROTO_BBB,
1689 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x\n",
1690 device_get_nameunit(sc->sc_dev), sc->proto));
1693 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1695 * For Reset Recovery the host shall issue in the following order:
1696 * a) a Bulk-Only Mass Storage Reset
1697 * b) a Clear Feature HALT to the Bulk-In endpoint
1698 * c) a Clear Feature HALT to the Bulk-Out endpoint
1700 * This is done in 3 steps, states:
1705 * If the reset doesn't succeed, the device should be port reset.
1708 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1709 device_get_nameunit(sc->sc_dev)));
1711 sc->transfer_state = TSTATE_BBB_RESET1;
1712 sc->transfer_status = status;
1714 usbd_interface2device_handle(sc->iface, &udev);
1716 /* reset is a class specific interface write */
1717 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1718 sc->request.bRequest = UR_BBB_RESET;
1719 USETW(sc->request.wValue, 0);
1720 USETW(sc->request.wIndex, sc->ifaceno);
1721 USETW(sc->request.wLength, 0);
1722 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1723 sc->transfer_xfer[XFER_BBB_RESET1]);
1727 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1728 void *data, int datalen, int dir, u_int timeout,
1729 transfer_cb_f cb, void *priv)
1731 KASSERT(sc->proto & UMASS_PROTO_BBB,
1732 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x\n",
1733 device_get_nameunit(sc->sc_dev), sc->proto));
1735 /* Be a little generous. */
1736 sc->timeout = timeout + UMASS_TIMEOUT;
1739 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1740 * a data phase of datalen bytes from/to the device and finally a
1742 * If the data direction was inbound a maximum of datalen bytes
1743 * is stored in the buffer pointed to by data.
1745 * umass_bbb_transfer initialises the transfer and lets the state
1746 * machine in umass_bbb_state handle the completion. It uses the
1748 * TSTATE_BBB_COMMAND
1749 * -> TSTATE_BBB_DATA
1750 * -> TSTATE_BBB_STATUS
1751 * -> TSTATE_BBB_STATUS2
1752 * -> TSTATE_BBB_IDLE
1754 * An error in any of those states will invoke
1758 /* check the given arguments */
1759 KASSERT(datalen == 0 || data != NULL,
1760 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1761 KASSERT(cmdlen <= CBWCDBLENGTH,
1762 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1763 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1764 KASSERT(dir == DIR_NONE || datalen > 0,
1765 ("%s: datalen == 0 while direction is not NONE\n",
1766 device_get_nameunit(sc->sc_dev)));
1767 KASSERT(datalen == 0 || dir != DIR_NONE,
1768 ("%s: direction is NONE while datalen is not zero\n",
1769 device_get_nameunit(sc->sc_dev)));
1770 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1771 ("%s: CBW struct does not have the right size (%ld vs. %d)\n",
1772 device_get_nameunit(sc->sc_dev),
1773 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1774 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1775 ("%s: CSW struct does not have the right size (%ld vs. %d)\n",
1776 device_get_nameunit(sc->sc_dev),
1777 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1780 * Determine the direction of the data transfer and the length.
1782 * dCBWDataTransferLength (datalen) :
1783 * This field indicates the number of bytes of data that the host
1784 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1785 * the Direction bit) during the execution of this command. If this
1786 * field is set to 0, the device will expect that no data will be
1787 * transferred IN or OUT during this command, regardless of the value
1788 * of the Direction bit defined in dCBWFlags.
1791 * The bits of the Flags field are defined as follows:
1793 * Bit 7 Direction - this bit shall be ignored if the
1794 * dCBWDataTransferLength field is zero.
1795 * 0 = data Out from host to device
1796 * 1 = data In from device to host
1799 /* Fill in the Command Block Wrapper
1800 * We fill in all the fields, so there is no need to bzero it first.
1802 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1803 /* We don't care about the initial value, as long as the values are unique */
1804 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1805 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1806 /* DIR_NONE is treated as DIR_OUT (0x00) */
1807 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1808 sc->cbw.bCBWLUN = lun;
1809 sc->cbw.bCDBLength = cmdlen;
1810 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1812 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1814 /* store the details for the data transfer phase */
1815 sc->transfer_dir = dir;
1816 sc->transfer_data = data;
1817 sc->transfer_datalen = datalen;
1818 sc->transfer_actlen = 0;
1819 sc->transfer_cb = cb;
1820 sc->transfer_priv = priv;
1821 sc->transfer_status = STATUS_CMD_OK;
1823 /* move from idle to the command state */
1824 sc->transfer_state = TSTATE_BBB_COMMAND;
1826 /* Send the CBW from host to device via bulk-out endpoint. */
1827 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1828 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1829 sc->transfer_xfer[XFER_BBB_CBW])) {
1830 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1836 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1839 struct umass_softc *sc = (struct umass_softc *) priv;
1840 usbd_xfer_handle next_xfer;
1842 KASSERT(sc->proto & UMASS_PROTO_BBB,
1843 ("%s: umass_bbb_state: wrong sc->proto 0x%02x\n",
1844 device_get_nameunit(sc->sc_dev), sc->proto));
1847 * State handling for BBB transfers.
1849 * The subroutine is rather long. It steps through the states given in
1850 * Annex A of the Bulk-Only specification.
1851 * Each state first does the error handling of the previous transfer
1852 * and then prepares the next transfer.
1853 * Each transfer is done asynchronously so after the request/transfer
1854 * has been submitted you will find a 'return;'.
1857 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1858 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1859 states[sc->transfer_state], xfer, usbd_errstr(err)));
1861 /* Give up if the device has detached. */
1862 if (sc->flags & UMASS_FLAGS_GONE) {
1863 sc->transfer_state = TSTATE_IDLE;
1864 sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
1869 switch (sc->transfer_state) {
1871 /***** Bulk Transfer *****/
1872 case TSTATE_BBB_COMMAND:
1873 /* Command transport phase, error handling */
1875 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1876 device_get_nameunit(sc->sc_dev)));
1877 /* If the device detects that the CBW is invalid, then
1878 * the device may STALL both bulk endpoints and require
1881 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1885 /* Data transport phase, setup transfer */
1886 sc->transfer_state = TSTATE_BBB_DATA;
1887 if (sc->transfer_dir == DIR_IN) {
1888 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1889 sc->transfer_data, sc->transfer_datalen,
1891 sc->transfer_xfer[XFER_BBB_DATA]))
1892 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1895 } else if (sc->transfer_dir == DIR_OUT) {
1896 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1897 sc->transfer_data, sc->transfer_datalen,
1898 0, /* fixed length transfer */
1899 sc->transfer_xfer[XFER_BBB_DATA]))
1900 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1904 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1905 device_get_nameunit(sc->sc_dev)));
1908 /* FALLTHROUGH if no data phase, err == 0 */
1909 case TSTATE_BBB_DATA:
1910 /* Command transport phase, error handling (ignored if no data
1911 * phase (fallthrough from previous state)) */
1912 if (sc->transfer_dir != DIR_NONE) {
1913 /* retrieve the length of the transfer that was done */
1914 usbd_get_xfer_status(xfer, NULL, NULL,
1915 &sc->transfer_actlen, NULL);
1918 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
1919 "%s\n", device_get_nameunit(sc->sc_dev),
1920 (sc->transfer_dir == DIR_IN?"in":"out"),
1921 sc->transfer_datalen,usbd_errstr(err)));
1923 if (err == USBD_STALLED) {
1924 umass_clear_endpoint_stall(sc,
1925 (sc->transfer_dir == DIR_IN?
1926 sc->bulkin:sc->bulkout),
1927 (sc->transfer_dir == DIR_IN?
1928 sc->bulkin_pipe:sc->bulkout_pipe),
1930 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1933 /* Unless the error is a pipe stall the
1936 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1942 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1943 umass_dump_buffer(sc, sc->transfer_data,
1944 sc->transfer_datalen, 48));
1948 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1949 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1950 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1951 /* Reading of CSW after bulk stall condition in data phase
1952 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1953 * reading CSW (TSTATE_BBB_SCLEAR).
1954 * In the case of no data phase or successfull data phase,
1955 * err == 0 and the following if block is passed.
1957 if (err) { /* should not occur */
1958 /* try the transfer below, even if clear stall failed */
1959 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1960 ", %s\n", device_get_nameunit(sc->sc_dev),
1961 (sc->transfer_dir == DIR_IN? "in":"out"),
1963 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1967 /* Status transport phase, setup transfer */
1968 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1969 sc->transfer_state == TSTATE_BBB_DATA ||
1970 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1971 /* After no data phase, successfull data phase and
1972 * after clearing bulk-in/-out stall condition
1974 sc->transfer_state = TSTATE_BBB_STATUS1;
1975 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1977 /* After first attempt of fetching CSW */
1978 sc->transfer_state = TSTATE_BBB_STATUS2;
1979 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1982 /* Read the Command Status Wrapper via bulk-in endpoint. */
1983 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1984 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
1986 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1991 case TSTATE_BBB_STATUS1: /* first attempt */
1992 case TSTATE_BBB_STATUS2: /* second attempt */
1993 /* Status transfer, error handling */
1995 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1996 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
1997 (sc->transfer_state == TSTATE_BBB_STATUS1?
2000 /* If this was the first attempt at fetching the CSW
2001 * retry it, otherwise fail.
2003 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
2004 umass_clear_endpoint_stall(sc,
2005 sc->bulkin, sc->bulkin_pipe,
2007 sc->transfer_xfer[XFER_BBB_SCLEAR]);
2010 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2015 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2017 /* Translate weird command-status signatures. */
2018 if (sc->quirks & WRONG_CSWSIG) {
2019 u_int32_t dCSWSignature = UGETDW(sc->csw.dCSWSignature);
2020 if (dCSWSignature == CSWSIGNATURE_OLYMPUS_C1 ||
2021 dCSWSignature == CSWSIGNATURE_IMAGINATION_DBX1)
2022 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2026 Residue = UGETDW(sc->csw.dCSWDataResidue);
2028 sc->transfer_datalen - sc->transfer_actlen != 0)
2029 Residue = sc->transfer_datalen - sc->transfer_actlen;
2031 /* Check CSW and handle any error */
2032 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2033 /* Invalid CSW: Wrong signature or wrong tag might
2034 * indicate that the device is confused -> reset it.
2036 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
2037 device_get_nameunit(sc->sc_dev),
2038 UGETDW(sc->csw.dCSWSignature),
2041 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2043 } else if (UGETDW(sc->csw.dCSWTag)
2044 != UGETDW(sc->cbw.dCBWTag)) {
2045 printf("%s: Invalid CSW: tag %d should be %d\n",
2046 device_get_nameunit(sc->sc_dev),
2047 UGETDW(sc->csw.dCSWTag),
2048 UGETDW(sc->cbw.dCBWTag));
2050 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2053 /* CSW is valid here */
2054 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2055 printf("%s: Invalid CSW: status %d > %d\n",
2056 device_get_nameunit(sc->sc_dev),
2060 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2062 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2063 printf("%s: Phase Error, residue = %d\n",
2064 device_get_nameunit(sc->sc_dev), Residue);
2066 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2069 } else if (sc->transfer_actlen > sc->transfer_datalen) {
2070 /* Buffer overrun! Don't let this go by unnoticed */
2071 panic("%s: transferred %db instead of %db",
2072 device_get_nameunit(sc->sc_dev),
2073 sc->transfer_actlen, sc->transfer_datalen);
2075 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2076 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
2077 device_get_nameunit(sc->sc_dev), Residue));
2079 /* SCSI command failed but transfer was succesful */
2080 sc->transfer_state = TSTATE_IDLE;
2081 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2085 } else { /* success */
2086 sc->transfer_state = TSTATE_IDLE;
2087 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2093 /***** Bulk Reset *****/
2094 case TSTATE_BBB_RESET1:
2096 printf("%s: BBB reset failed, %s\n",
2097 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2099 umass_clear_endpoint_stall(sc,
2100 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
2101 sc->transfer_xfer[XFER_BBB_RESET2]);
2104 case TSTATE_BBB_RESET2:
2105 if (err) /* should not occur */
2106 printf("%s: BBB bulk-in clear stall failed, %s\n",
2107 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2108 /* no error recovery, otherwise we end up in a loop */
2110 umass_clear_endpoint_stall(sc,
2111 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
2112 sc->transfer_xfer[XFER_BBB_RESET3]);
2115 case TSTATE_BBB_RESET3:
2116 if (err) /* should not occur */
2117 printf("%s: BBB bulk-out clear stall failed, %s\n",
2118 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2119 /* no error recovery, otherwise we end up in a loop */
2121 sc->transfer_state = TSTATE_IDLE;
2122 if (sc->transfer_priv) {
2123 sc->transfer_cb(sc, sc->transfer_priv,
2124 sc->transfer_datalen,
2125 sc->transfer_status);
2130 /***** Default *****/
2132 panic("%s: Unknown state %d",
2133 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2138 umass_bbb_get_max_lun(struct umass_softc *sc)
2140 usbd_device_handle udev;
2141 usb_device_request_t req;
2143 usb_interface_descriptor_t *id;
2147 usbd_interface2device_handle(sc->iface, &udev);
2148 id = usbd_get_interface_descriptor(sc->iface);
2150 /* The Get Max Lun command is a class-specific request. */
2151 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2152 req.bRequest = UR_BBB_GET_MAX_LUN;
2153 USETW(req.wValue, 0);
2154 USETW(req.wIndex, id->bInterfaceNumber);
2155 USETW(req.wLength, 1);
2157 err = usbd_do_request(udev, &req, &buf);
2159 case USBD_NORMAL_COMPLETION:
2161 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
2162 device_get_nameunit(sc->sc_dev), maxlun));
2165 case USBD_SHORT_XFER:
2167 /* Device doesn't support Get Max Lun request. */
2168 printf("%s: Get Max Lun not supported (%s)\n",
2169 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2170 /* XXX Should we port_reset the device? */
2178 * Command/Bulk/Interrupt (CBI) specific functions
2182 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
2183 usbd_xfer_handle xfer)
2185 usbd_device_handle udev;
2187 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2188 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x\n",
2189 device_get_nameunit(sc->sc_dev), sc->proto));
2191 usbd_interface2device_handle(sc->iface, &udev);
2193 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2194 sc->request.bRequest = UR_CBI_ADSC;
2195 USETW(sc->request.wValue, 0);
2196 USETW(sc->request.wIndex, sc->ifaceno);
2197 USETW(sc->request.wLength, buflen);
2198 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
2204 umass_cbi_reset(struct umass_softc *sc, int status)
2207 # define SEND_DIAGNOSTIC_CMDLEN 12
2209 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2210 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x\n",
2211 device_get_nameunit(sc->sc_dev), sc->proto));
2214 * Command Block Reset Protocol
2216 * First send a reset request to the device. Then clear
2217 * any possibly stalled bulk endpoints.
2219 * This is done in 3 steps, states:
2224 * If the reset doesn't succeed, the device should be port reset.
2227 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
2228 device_get_nameunit(sc->sc_dev)));
2230 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
2231 ("%s: CBL struct is too small (%ld < %d)\n",
2232 device_get_nameunit(sc->sc_dev),
2233 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
2235 sc->transfer_state = TSTATE_CBI_RESET1;
2236 sc->transfer_status = status;
2238 /* The 0x1d code is the SEND DIAGNOSTIC command. To distinguish between
2239 * the two the last 10 bytes of the cbl is filled with 0xff (section
2240 * 2.2 of the CBI spec).
2242 sc->cbl[0] = 0x1d; /* Command Block Reset */
2244 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
2247 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
2248 sc->transfer_xfer[XFER_CBI_RESET1]);
2249 /* XXX if the command fails we should reset the port on the hub */
2253 umass_cbi_transfer(struct umass_softc *sc, int lun,
2254 void *cmd, int cmdlen, void *data, int datalen, int dir,
2255 u_int timeout, transfer_cb_f cb, void *priv)
2257 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2258 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x\n",
2259 device_get_nameunit(sc->sc_dev), sc->proto));
2261 /* Be a little generous. */
2262 sc->timeout = timeout + UMASS_TIMEOUT;
2265 * Do a CBI transfer with cmdlen bytes from cmd, possibly
2266 * a data phase of datalen bytes from/to the device and finally a
2268 * If the data direction was inbound a maximum of datalen bytes
2269 * is stored in the buffer pointed to by data.
2271 * umass_cbi_transfer initialises the transfer and lets the state
2272 * machine in umass_cbi_state handle the completion. It uses the
2274 * TSTATE_CBI_COMMAND
2277 * An error in any of those states will invoke
2281 /* check the given arguments */
2282 KASSERT(datalen == 0 || data != NULL,
2283 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
2284 KASSERT(datalen == 0 || dir != DIR_NONE,
2285 ("%s: direction is NONE while datalen is not zero\n",
2286 device_get_nameunit(sc->sc_dev)));
2288 /* store the details for the data transfer phase */
2289 sc->transfer_dir = dir;
2290 sc->transfer_data = data;
2291 sc->transfer_datalen = datalen;
2292 sc->transfer_actlen = 0;
2293 sc->transfer_cb = cb;
2294 sc->transfer_priv = priv;
2295 sc->transfer_status = STATUS_CMD_OK;
2297 /* move from idle to the command state */
2298 sc->transfer_state = TSTATE_CBI_COMMAND;
2300 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
2302 /* Send the Command Block from host to device via control endpoint. */
2303 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2304 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2308 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2311 struct umass_softc *sc = (struct umass_softc *) priv;
2313 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2314 ("%s: umass_cbi_state: wrong sc->proto 0x%02x\n",
2315 device_get_nameunit(sc->sc_dev), sc->proto));
2318 * State handling for CBI transfers.
2321 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2322 device_get_nameunit(sc->sc_dev), sc->transfer_state,
2323 states[sc->transfer_state], xfer, usbd_errstr(err)));
2325 /* Give up if the device has detached. */
2326 if (sc->flags & UMASS_FLAGS_GONE) {
2327 sc->transfer_state = TSTATE_IDLE;
2328 sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
2333 switch (sc->transfer_state) {
2335 /***** CBI Transfer *****/
2336 case TSTATE_CBI_COMMAND:
2337 if (err == USBD_STALLED) {
2338 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2339 device_get_nameunit(sc->sc_dev)));
2340 /* Status transport by control pipe (section 2.3.2.1).
2341 * The command contained in the command block failed.
2343 * The control pipe has already been unstalled by the
2345 * Section 2.4.3.1.1 states that the bulk in endpoints
2346 * should not be stalled at this point.
2349 sc->transfer_state = TSTATE_IDLE;
2350 sc->transfer_cb(sc, sc->transfer_priv,
2351 sc->transfer_datalen,
2356 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2357 device_get_nameunit(sc->sc_dev)));
2358 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2363 sc->transfer_state = TSTATE_CBI_DATA;
2364 if (sc->transfer_dir == DIR_IN) {
2365 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2366 sc->transfer_data, sc->transfer_datalen,
2368 sc->transfer_xfer[XFER_CBI_DATA]))
2369 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2371 } else if (sc->transfer_dir == DIR_OUT) {
2372 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2373 sc->transfer_data, sc->transfer_datalen,
2374 0, /* fixed length transfer */
2375 sc->transfer_xfer[XFER_CBI_DATA]))
2376 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2378 } else if (sc->proto & UMASS_PROTO_CBI_I) {
2379 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2380 device_get_nameunit(sc->sc_dev)));
2381 sc->transfer_state = TSTATE_CBI_STATUS;
2382 if (umass_setup_transfer(sc, sc->intrin_pipe,
2383 &sc->sbl, sizeof(sc->sbl),
2384 0, /* fixed length transfer */
2385 sc->transfer_xfer[XFER_CBI_STATUS])){
2386 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2389 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2390 device_get_nameunit(sc->sc_dev)));
2391 /* No command completion interrupt. Request
2394 sc->transfer_state = TSTATE_IDLE;
2395 sc->transfer_cb(sc, sc->transfer_priv,
2396 0, STATUS_CMD_UNKNOWN);
2401 case TSTATE_CBI_DATA:
2402 /* retrieve the length of the transfer that was done */
2403 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2406 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2407 "%s\n", device_get_nameunit(sc->sc_dev),
2408 (sc->transfer_dir == DIR_IN?"in":"out"),
2409 sc->transfer_datalen,usbd_errstr(err)));
2411 if (err == USBD_STALLED) {
2412 umass_clear_endpoint_stall(sc,
2413 sc->bulkin, sc->bulkin_pipe,
2415 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2417 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2422 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2423 umass_dump_buffer(sc, sc->transfer_data,
2424 sc->transfer_actlen, 48));
2426 if (sc->proto & UMASS_PROTO_CBI_I) {
2427 sc->transfer_state = TSTATE_CBI_STATUS;
2428 if (umass_setup_transfer(sc, sc->intrin_pipe,
2429 &sc->sbl, sizeof(sc->sbl),
2430 0, /* fixed length transfer */
2431 sc->transfer_xfer[XFER_CBI_STATUS])){
2432 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2435 /* No command completion interrupt. Request
2436 * sense to get status of command.
2438 sc->transfer_state = TSTATE_IDLE;
2439 sc->transfer_cb(sc, sc->transfer_priv,
2440 sc->transfer_datalen - sc->transfer_actlen,
2441 STATUS_CMD_UNKNOWN);
2445 case TSTATE_CBI_STATUS:
2447 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2448 device_get_nameunit(sc->sc_dev)));
2449 /* Status transport by interrupt pipe (section 2.3.2.2).
2452 if (err == USBD_STALLED) {
2453 umass_clear_endpoint_stall(sc,
2454 sc->intrin, sc->intrin_pipe,
2456 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2458 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2463 /* Dissect the information in the buffer */
2465 if (sc->proto & UMASS_PROTO_UFI) {
2468 /* Section 3.4.3.1.3 specifies that the UFI command
2469 * protocol returns an ASC and ASCQ in the interrupt
2473 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2475 device_get_nameunit(sc->sc_dev),
2476 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2478 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2479 status = STATUS_CMD_OK;
2481 status = STATUS_CMD_FAILED;
2483 sc->transfer_state = TSTATE_IDLE;
2484 sc->transfer_cb(sc, sc->transfer_priv,
2485 sc->transfer_datalen - sc->transfer_actlen,
2488 /* Command Interrupt Data Block */
2489 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2490 device_get_nameunit(sc->sc_dev),
2491 sc->sbl.common.type, sc->sbl.common.value));
2493 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2496 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2497 == IDB_VALUE_PASS) {
2498 err = STATUS_CMD_OK;
2499 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2500 == IDB_VALUE_FAIL ||
2501 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2502 == IDB_VALUE_PERSISTENT) {
2503 err = STATUS_CMD_FAILED;
2505 err = STATUS_WIRE_FAILED;
2508 sc->transfer_state = TSTATE_IDLE;
2509 sc->transfer_cb(sc, sc->transfer_priv,
2510 sc->transfer_datalen-sc->transfer_actlen,
2516 case TSTATE_CBI_DCLEAR:
2517 if (err) { /* should not occur */
2518 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
2519 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2520 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2523 sc->transfer_state = TSTATE_IDLE;
2524 sc->transfer_cb(sc, sc->transfer_priv,
2525 sc->transfer_datalen,
2529 case TSTATE_CBI_SCLEAR:
2530 if (err) /* should not occur */
2531 printf("%s: CBI intr-in stall clear failed, %s\n",
2532 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2534 /* Something really bad is going on. Reset the device */
2535 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2538 /***** CBI Reset *****/
2539 case TSTATE_CBI_RESET1:
2541 printf("%s: CBI reset failed, %s\n",
2542 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2544 umass_clear_endpoint_stall(sc,
2545 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2546 sc->transfer_xfer[XFER_CBI_RESET2]);
2549 case TSTATE_CBI_RESET2:
2550 if (err) /* should not occur */
2551 printf("%s: CBI bulk-in stall clear failed, %s\n",
2552 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2553 /* no error recovery, otherwise we end up in a loop */
2555 umass_clear_endpoint_stall(sc,
2556 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2557 sc->transfer_xfer[XFER_CBI_RESET3]);
2560 case TSTATE_CBI_RESET3:
2561 if (err) /* should not occur */
2562 printf("%s: CBI bulk-out stall clear failed, %s\n",
2563 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2564 /* no error recovery, otherwise we end up in a loop */
2566 sc->transfer_state = TSTATE_IDLE;
2567 if (sc->transfer_priv) {
2568 sc->transfer_cb(sc, sc->transfer_priv,
2569 sc->transfer_datalen,
2570 sc->transfer_status);
2576 /***** Default *****/
2578 panic("%s: Unknown state %d",
2579 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2587 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2591 umass_cam_attach_sim(struct umass_softc *sc)
2593 struct cam_devq *devq; /* Per device Queue */
2595 /* A HBA is attached to the CAM layer.
2597 * The CAM layer will then after a while start probing for
2598 * devices on the bus. The number of SIMs is limited to one.
2601 devq = cam_simq_alloc(1 /*maximum openings*/);
2605 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2608 device_get_unit(sc->sc_dev) /*unit number*/,
2610 1 /*maximum device openings*/,
2611 0 /*maximum tagged device openings*/,
2613 if (sc->umass_sim == NULL) {
2614 cam_simq_free(devq);
2618 if(xpt_bus_register(sc->umass_sim, NULL, device_get_unit(sc->sc_dev)) !=
2626 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2629 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2630 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2631 periph->periph_name, periph->unit_number,
2632 ccb->ccb_h.status));
2634 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2635 periph->periph_name, periph->unit_number));
2639 xpt_free_path(ccb->ccb_h.path);
2640 free(ccb, M_USBDEV);
2644 umass_cam_rescan(void *addr)
2646 struct umass_softc *sc = (struct umass_softc *) addr;
2647 struct cam_path *path;
2650 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2651 cam_sim_path(sc->umass_sim),
2652 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2653 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2655 ccb = malloc(sizeof(union ccb), M_USBDEV, M_NOWAIT | M_ZERO);
2658 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2659 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD)
2661 free(ccb, M_USBDEV);
2665 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2666 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2667 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2668 ccb->crcn.flags = CAM_FLAG_NONE;
2671 /* The scan is in progress now. */
2675 umass_cam_attach(struct umass_softc *sc)
2680 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2681 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2682 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2683 cam_sim_path(sc->umass_sim));
2686 /* Notify CAM of the new device after a short delay. Any
2687 * failure is benign, as the user can still do it by hand
2688 * (camcontrol rescan <busno>). Only do this if we are not
2689 * booting, because CAM does a scan after booting has
2690 * completed, when interrupts have been enabled.
2693 callout_reset(&sc->cam_scsi_rescan_ch, MS_TO_TICKS(200),
2694 umass_cam_rescan, sc);
2697 return(0); /* always succesfull */
2701 * detach from the CAM layer
2705 umass_cam_detach_sim(struct umass_softc *sc)
2707 if (sc->umass_sim) {
2708 if (xpt_bus_deregister(cam_sim_path(sc->umass_sim)))
2709 cam_sim_free(sc->umass_sim, /*free_devq*/TRUE);
2713 sc->umass_sim = NULL;
2720 * CAM requests for action come through here
2724 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2726 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2728 /* The softc is still there, but marked as going away. umass_cam_detach
2729 * has not yet notified CAM of the lost device however.
2731 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2732 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2733 "Invalid target (gone)\n",
2734 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2735 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2736 ccb->ccb_h.func_code));
2737 ccb->ccb_h.status = CAM_TID_INVALID;
2742 /* Verify, depending on the operation to perform, that we either got a
2743 * valid sc, because an existing target was referenced, or otherwise
2744 * the SIM is addressed.
2746 * This avoids bombing out at a printf and does give the CAM layer some
2747 * sensible feedback on errors.
2749 switch (ccb->ccb_h.func_code) {
2752 case XPT_GET_TRAN_SETTINGS:
2753 case XPT_SET_TRAN_SETTINGS:
2754 case XPT_CALC_GEOMETRY:
2755 /* the opcodes requiring a target. These should never occur. */
2757 printf("%s:%d:%d:%d:func_code 0x%04x: "
2758 "Invalid target (target needed)\n",
2759 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2760 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2761 ccb->ccb_h.func_code);
2763 ccb->ccb_h.status = CAM_TID_INVALID;
2770 /* The opcodes sometimes aimed at a target (sc is valid),
2771 * sometimes aimed at the SIM (sc is invalid and target is
2772 * CAM_TARGET_WILDCARD)
2774 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2775 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2776 "Invalid target (no wildcard)\n",
2777 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2778 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2779 ccb->ccb_h.func_code));
2781 ccb->ccb_h.status = CAM_TID_INVALID;
2787 /* XXX Hm, we should check the input parameters */
2791 /* Perform the requested action */
2792 switch (ccb->ccb_h.func_code) {
2795 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2799 unsigned char *rcmd;
2802 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2803 "cmd: 0x%02x, flags: 0x%02x, "
2804 "%db cmd/%db data/%db sense\n",
2805 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2806 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2807 csio->cdb_io.cdb_bytes[0],
2808 ccb->ccb_h.flags & CAM_DIR_MASK,
2809 csio->cdb_len, csio->dxfer_len,
2812 /* clear the end of the buffer to make sure we don't send out
2815 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2817 umass_dump_buffer(sc, csio->data_ptr,
2818 csio->dxfer_len, 48));
2820 if (sc->transfer_state != TSTATE_IDLE) {
2821 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2822 "I/O in progress, deferring (state %d, %s)\n",
2823 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2824 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2825 sc->transfer_state,states[sc->transfer_state]));
2826 ccb->ccb_h.status = CAM_SCSI_BUSY;
2831 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2842 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2845 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2846 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2848 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2850 cmdlen = csio->cdb_len;
2851 rcmd = (unsigned char *) &sc->cam_scsi_command;
2852 rcmdlen = sizeof(sc->cam_scsi_command);
2854 /* sc->transform will convert the command to the command
2855 * (format) needed by the specific command set and return
2856 * the converted command in a buffer pointed to be rcmd.
2857 * We pass in a buffer, but if the command does not
2858 * have to be transformed it returns a ptr to the original
2859 * buffer (see umass_scsi_transform).
2862 switch (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2865 * Handle EVPD inquiry for broken devices first
2866 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2868 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2869 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2870 struct scsi_sense_data *sense;
2872 sense = &ccb->csio.sense_data;
2873 bzero(sense, sizeof(*sense));
2874 sense->error_code = SSD_CURRENT_ERROR;
2875 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2876 sense->add_sense_code = 0x24;
2877 sense->extra_len = 10;
2878 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2879 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2884 /* Return fake inquiry data for broken devices */
2885 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
2886 struct ccb_scsiio *csio = &ccb->csio;
2888 memcpy(csio->data_ptr, &fake_inq_data,
2889 sizeof(fake_inq_data));
2890 csio->scsi_status = SCSI_STATUS_OK;
2891 ccb->ccb_h.status = CAM_REQ_CMP;
2895 if ((sc->quirks & NO_SYNCHRONIZE_CACHE) &&
2896 rcmd[0] == SYNCHRONIZE_CACHE) {
2897 struct ccb_scsiio *csio = &ccb->csio;
2899 csio->scsi_status = SCSI_STATUS_OK;
2900 ccb->ccb_h.status = CAM_REQ_CMP;
2904 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
2905 rcmd[0] == INQUIRY) {
2906 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
2908 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
2910 csio->dxfer_len, dir, ccb->ccb_h.timeout,
2911 umass_cam_cb, (void *) ccb);
2914 ccb->ccb_h.status = CAM_REQ_INVALID;
2918 ccb->ccb_h.status = CAM_REQ_CMP;
2927 struct ccb_pathinq *cpi = &ccb->cpi;
2929 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
2930 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
2931 cam_sim_path(sc->umass_sim),
2932 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2934 /* host specific information */
2935 cpi->version_num = 1;
2936 cpi->hba_inquiry = 0;
2937 cpi->target_sprt = 0;
2938 cpi->hba_misc = PIM_NO_6_BYTE;
2939 cpi->hba_eng_cnt = 0;
2940 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2941 cpi->initiator_id = UMASS_SCSIID_HOST;
2942 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2943 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2944 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2945 cpi->unit_number = cam_sim_unit(sim);
2946 cpi->bus_id = device_get_unit(sc->sc_dev);
2947 cpi->protocol = PROTO_SCSI;
2948 cpi->protocol_version = SCSI_REV_2;
2949 cpi->transport = XPORT_USB;
2950 cpi->transport_version = 0;
2953 cpi->base_transfer_speed = 0;
2956 if (sc->quirks & FLOPPY_SPEED) {
2957 cpi->base_transfer_speed =
2958 UMASS_FLOPPY_TRANSFER_SPEED;
2959 } else if (usbd_get_speed(sc->sc_udev) ==
2961 cpi->base_transfer_speed =
2962 UMASS_HIGH_TRANSFER_SPEED;
2964 cpi->base_transfer_speed =
2965 UMASS_FULL_TRANSFER_SPEED;
2967 cpi->max_lun = sc->maxlun;
2970 cpi->ccb_h.status = CAM_REQ_CMP;
2976 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
2977 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2978 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2980 ccb->ccb_h.status = CAM_REQ_INPROG;
2981 umass_reset(sc, umass_cam_cb, (void *) ccb);
2984 case XPT_GET_TRAN_SETTINGS:
2986 struct ccb_trans_settings *cts = &ccb->cts;
2987 cts->protocol = PROTO_SCSI;
2988 cts->protocol_version = SCSI_REV_2;
2989 cts->transport = XPORT_USB;
2990 cts->transport_version = 0;
2991 cts->xport_specific.valid = 0;
2994 ccb->ccb_h.status = CAM_REQ_CMP;
2998 case XPT_SET_TRAN_SETTINGS:
3000 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3001 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3002 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3004 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3008 case XPT_CALC_GEOMETRY:
3010 cam_calc_geometry(&ccb->ccg, /*extended*/1);
3016 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
3017 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3018 cam_sim_path(sc->umass_sim),
3019 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3021 ccb->ccb_h.status = CAM_REQ_CMP;
3026 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
3027 "Not implemented\n",
3028 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3029 cam_sim_path(sc->umass_sim),
3030 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
3031 ccb->ccb_h.func_code));
3033 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3040 umass_cam_poll(struct cam_sim *sim)
3042 struct umass_softc *sc = (struct umass_softc *) sim->softc;
3044 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
3045 device_get_nameunit(sc->sc_dev)));
3047 usbd_set_polling(sc->sc_udev, 1);
3048 usbd_dopoll(sc->iface);
3049 usbd_set_polling(sc->sc_udev, 0);
3054 * finalise a completed CAM command
3058 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
3060 union ccb *ccb = (union ccb *) priv;
3061 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3063 /* If the device is gone, just fail the request. */
3064 if (sc->flags & UMASS_FLAGS_GONE) {
3065 ccb->ccb_h.status = CAM_TID_INVALID;
3070 csio->resid = residue;
3074 ccb->ccb_h.status = CAM_REQ_CMP;
3075 if ((sc->quirks & READ_CAPACITY_OFFBY1) &&
3076 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3077 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3078 struct scsi_read_capacity_data *rcap;
3081 rcap = (struct scsi_read_capacity_data *)csio->data_ptr;
3082 maxsector = scsi_4btoul(rcap->addr) - 1;
3083 scsi_ulto4b(maxsector, rcap->addr);
3088 case STATUS_CMD_UNKNOWN:
3089 case STATUS_CMD_FAILED:
3090 switch (ccb->ccb_h.func_code) {
3093 unsigned char *rcmd;
3096 /* fetch sense data */
3097 /* the rest of the command was filled in at attach */
3098 sc->cam_scsi_sense.length = csio->sense_len;
3100 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
3101 device_get_nameunit(sc->sc_dev), csio->sense_len));
3103 rcmd = (unsigned char *) &sc->cam_scsi_command;
3104 rcmdlen = sizeof(sc->cam_scsi_command);
3106 if (sc->transform(sc,
3107 (unsigned char *) &sc->cam_scsi_sense,
3108 sizeof(sc->cam_scsi_sense),
3109 &rcmd, &rcmdlen) == 1) {
3110 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
3111 csio->sense_len = SHORT_INQUIRY_LENGTH;
3113 sc->transfer(sc, ccb->ccb_h.target_lun,
3116 csio->sense_len, DIR_IN, ccb->ccb_h.timeout,
3117 umass_cam_sense_cb, (void *) ccb);
3119 panic("transform(REQUEST_SENSE) failed");
3123 case XPT_RESET_DEV: /* Reset failed */
3124 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3128 panic("umass_cam_cb called for func_code %d",
3129 ccb->ccb_h.func_code);
3133 case STATUS_WIRE_FAILED:
3134 /* the wire protocol failed and will have recovered
3135 * (hopefully). We return an error to CAM and let CAM retry
3136 * the command if necessary.
3138 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3142 panic("%s: Unknown status %d in umass_cam_cb",
3143 device_get_nameunit(sc->sc_dev), status);
3147 /* Finalise a completed autosense operation
3150 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
3152 union ccb *ccb = (union ccb *) priv;
3153 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3154 unsigned char *rcmd;
3157 if (sc->flags & UMASS_FLAGS_GONE) {
3158 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3165 case STATUS_CMD_UNKNOWN:
3166 case STATUS_CMD_FAILED:
3167 /* Getting sense data always succeeds (apart from wire
3170 if ((sc->quirks & RS_NO_CLEAR_UA)
3171 && csio->cdb_io.cdb_bytes[0] == INQUIRY
3172 && (csio->sense_data.flags & SSD_KEY)
3173 == SSD_KEY_UNIT_ATTENTION) {
3174 /* Ignore unit attention errors in the case where
3175 * the Unit Attention state is not cleared on
3176 * REQUEST SENSE. They will appear again at the next
3179 ccb->ccb_h.status = CAM_REQ_CMP;
3180 } else if ((csio->sense_data.flags & SSD_KEY)
3181 == SSD_KEY_NO_SENSE) {
3182 /* No problem after all (in the case of CBI without
3185 ccb->ccb_h.status = CAM_REQ_CMP;
3186 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
3187 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
3188 ((csio->sense_data.flags & SSD_KEY)
3189 == SSD_KEY_UNIT_ATTENTION)) {
3191 * Some devices do not clear the unit attention error
3192 * on request sense. We insert a test unit ready
3193 * command to make sure we clear the unit attention
3194 * condition, then allow the retry to proceed as
3198 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3199 | CAM_AUTOSNS_VALID;
3200 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3206 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
3207 "TEST_UNIT_READY\n",
3208 device_get_nameunit(sc->sc_dev)));
3210 /* the rest of the command was filled in at attach */
3212 rcmd = (unsigned char *) &sc->cam_scsi_command2;
3213 rcmdlen = sizeof(sc->cam_scsi_command2);
3215 if (sc->transform(sc,
3217 &sc->cam_scsi_test_unit_ready,
3218 sizeof(sc->cam_scsi_test_unit_ready),
3219 &rcmd, &rcmdlen) == 1) {
3220 sc->transfer(sc, ccb->ccb_h.target_lun,
3222 NULL, 0, DIR_NONE, ccb->ccb_h.timeout,
3223 umass_cam_quirk_cb, (void *) ccb);
3225 panic("transform(TEST_UNIT_READY) failed");
3229 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3230 | CAM_AUTOSNS_VALID;
3231 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3237 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3238 device_get_nameunit(sc->sc_dev), status));
3239 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3245 * This completion code just handles the fact that we sent a test-unit-ready
3246 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3247 * though this command succeeded, we have to tell CAM to retry.
3250 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
3252 union ccb *ccb = (union ccb *) priv;
3254 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
3255 device_get_nameunit(sc->sc_dev), status));
3257 if (sc->flags & UMASS_FLAGS_GONE) {
3258 ccb->ccb_h.status = CAM_TID_INVALID;
3263 ccb->ccb_h.status = CAM_REQ_CMP;
3265 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3266 | CAM_AUTOSNS_VALID;
3267 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3272 umass_driver_load(module_t mod, int what, void *arg)
3278 return(usbd_driver_load(mod, what, arg));
3283 * SCSI specific functions
3287 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3288 unsigned char **rcmd, int *rcmdlen)
3291 case TEST_UNIT_READY:
3292 if (sc->quirks & NO_TEST_UNIT_READY) {
3293 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3294 ("rcmdlen = %d < %ld, buffer too small",
3296 (long)sizeof(struct scsi_start_stop_unit)));
3297 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3298 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3299 memset(*rcmd, 0, *rcmdlen);
3300 (*rcmd)[0] = START_STOP_UNIT;
3301 (*rcmd)[4] = SSS_START;
3306 /* some drives wedge when asked for full inquiry information. */
3307 if (sc->quirks & FORCE_SHORT_INQUIRY) {
3308 memcpy(*rcmd, cmd, cmdlen);
3310 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3315 *rcmd = cmd; /* We don't need to copy it */
3321 /* RBC specific functions */
3323 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3324 unsigned char **rcmd, int *rcmdlen)
3327 /* these commands are defined in RBC: */
3330 case START_STOP_UNIT:
3331 case SYNCHRONIZE_CACHE:
3333 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
3335 case MODE_SELECT_10:
3337 case TEST_UNIT_READY:
3339 /* The following commands are not listed in my copy of the RBC specs.
3340 * CAM however seems to want those, and at least the Sony DSC device
3341 * appears to support those as well */
3344 if ((sc->quirks & RBC_PAD_TO_12) && cmdlen < 12) {
3346 bcopy(cmd, *rcmd, cmdlen);
3347 bzero(*rcmd + cmdlen, 12 - cmdlen);
3349 *rcmd = cmd; /* We don't need to copy it */
3353 /* All other commands are not legal in RBC */
3355 printf("%s: Unsupported RBC command 0x%02x",
3356 device_get_nameunit(sc->sc_dev), cmd[0]);
3358 return 0; /* failure */
3363 * UFI specific functions
3366 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3367 unsigned char **rcmd, int *rcmdlen)
3369 /* A UFI command is always 12 bytes in length */
3370 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
3371 ("rcmdlen = %d < %d, buffer too small",
3372 *rcmdlen, UFI_COMMAND_LENGTH));
3374 *rcmdlen = UFI_COMMAND_LENGTH;
3375 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
3378 /* Commands of which the format has been verified. They should work.
3379 * Copy the command into the (zeroed out) destination buffer.
3381 case TEST_UNIT_READY:
3382 if (sc->quirks & NO_TEST_UNIT_READY) {
3383 /* Some devices do not support this command.
3384 * Start Stop Unit should give the same results
3386 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3387 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3388 (*rcmd)[0] = START_STOP_UNIT;
3389 (*rcmd)[4] = SSS_START;
3391 memcpy(*rcmd, cmd, cmdlen);
3399 case START_STOP_UNIT:
3400 case SEND_DIAGNOSTIC:
3405 case POSITION_TO_ELEMENT: /* SEEK_10 */
3406 case WRITE_AND_VERIFY:
3408 case MODE_SELECT_10:
3412 case READ_FORMAT_CAPACITIES:
3413 memcpy(*rcmd, cmd, cmdlen);
3417 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3418 * required for UFI devices, so it is appropriate to fake
3421 case SYNCHRONIZE_CACHE:
3425 printf("%s: Unsupported UFI command 0x%02x\n",
3426 device_get_nameunit(sc->sc_dev), cmd[0]);
3427 return 0; /* failure */
3432 * 8070i (ATAPI) specific functions
3435 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3436 unsigned char **rcmd, int *rcmdlen)
3438 /* An ATAPI command is always 12 bytes in length. */
3439 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
3440 ("rcmdlen = %d < %d, buffer too small",
3441 *rcmdlen, ATAPI_COMMAND_LENGTH));
3443 *rcmdlen = ATAPI_COMMAND_LENGTH;
3444 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
3447 /* Commands of which the format has been verified. They should work.
3448 * Copy the command into the (zeroed out) destination buffer.
3451 memcpy(*rcmd, cmd, cmdlen);
3452 /* some drives wedge when asked for full inquiry information. */
3453 if (sc->quirks & FORCE_SHORT_INQUIRY)
3454 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3457 case TEST_UNIT_READY:
3458 if (sc->quirks & NO_TEST_UNIT_READY) {
3459 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3460 ("rcmdlen = %d < %ld, buffer too small",
3462 (long)sizeof(struct scsi_start_stop_unit)));
3463 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3464 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3465 memset(*rcmd, 0, *rcmdlen);
3466 (*rcmd)[0] = START_STOP_UNIT;
3467 (*rcmd)[4] = SSS_START;
3473 case START_STOP_UNIT:
3474 case SEND_DIAGNOSTIC:
3479 case POSITION_TO_ELEMENT: /* SEEK_10 */
3480 case SYNCHRONIZE_CACHE:
3481 case MODE_SELECT_10:
3484 case 0x42: /* READ_SUBCHANNEL */
3485 case 0x43: /* READ_TOC */
3486 case 0x44: /* READ_HEADER */
3487 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
3488 case 0x48: /* PLAY_TRACK */
3489 case 0x49: /* PLAY_TRACK_REL */
3490 case 0x4b: /* PAUSE */
3491 case 0x51: /* READ_DISK_INFO */
3492 case 0x52: /* READ_TRACK_INFO */
3493 case 0x54: /* SEND_OPC */
3494 case 0x59: /* READ_MASTER_CUE */
3495 case 0x5b: /* CLOSE_TR_SESSION */
3496 case 0x5c: /* READ_BUFFER_CAP */
3497 case 0x5d: /* SEND_CUE_SHEET */
3498 case 0xa1: /* BLANK */
3499 case 0xa5: /* PLAY_12 */
3500 case 0xa6: /* EXCHANGE_MEDIUM */
3501 case 0xad: /* READ_DVD_STRUCTURE */
3502 case 0xbb: /* SET_CD_SPEED */
3503 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
3504 memcpy(*rcmd, cmd, cmdlen);
3510 printf("%s: Unsupported ATAPI command 0x%02x"
3511 " - trying anyway\n",
3512 device_get_nameunit(sc->sc_dev), cmd[0]);
3513 memcpy(*rcmd, cmd, cmdlen);
3519 /* (even the comment is missing) */
3521 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
3527 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3529 int clen = cbw->bCDBLength;
3530 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3531 u_int8_t *c = cbw->CBWCDB;
3532 int tag = UGETDW(cbw->dCBWTag);
3533 int flags = cbw->bCBWFlags;
3535 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3536 "(0x%02x%02x%02x%02x%02x%02x%s), "
3537 "data = %db, dir = %s\n",
3538 device_get_nameunit(sc->sc_dev), tag, clen,
3539 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3540 dlen, (flags == CBWFLAGS_IN? "in":
3541 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3545 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3547 int sig = UGETDW(csw->dCSWSignature);
3548 int tag = UGETW(csw->dCSWTag);
3549 int res = UGETDW(csw->dCSWDataResidue);
3550 int status = csw->bCSWStatus;
3552 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3553 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3554 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3556 status, (status == CSWSTATUS_GOOD? "good":
3557 (status == CSWSTATUS_FAILED? "failed":
3558 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3562 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3565 int dir = sc->transfer_dir;
3567 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3568 "(0x%02x%02x%02x%02x%02x%02x%s), "
3569 "data = %db, dir = %s\n",
3570 device_get_nameunit(sc->sc_dev), cmdlen,
3571 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3572 sc->transfer_datalen,
3573 (dir == DIR_IN? "in":
3574 (dir == DIR_OUT? "out":
3575 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3579 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3590 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3591 for (i = 0; i < buflen && i < printlen; i++) {
3593 if (j == 0 && i != 0) {
3594 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3595 device_get_nameunit(sc->sc_dev), s1, s2));
3598 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3600 if (buflen > printlen)
3601 sprintf(s3, " ...");
3602 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3603 device_get_nameunit(sc->sc_dev), s1, s2, s3));