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_ALCOR, USB_PRODUCT_ALCOR_TRANSCEND, RID_WILDCARD,
344 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
347 { USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_UMCR_9361, RID_WILDCARD,
348 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
351 { USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO230, RID_WILDCARD,
352 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
355 { USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO330, RID_WILDCARD,
356 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
359 { USB_VENDOR_ASAHIOPTICAL, PID_WILDCARD, RID_WILDCARD,
360 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
363 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_ATTACHE, RID_WILDCARD,
364 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
367 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_A256MB, RID_WILDCARD,
368 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
371 { USB_VENDOR_ADDON, USB_PRODUCT_ADDON_DISKPRO512, RID_WILDCARD,
372 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
375 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2SCSI, RID_WILDCARD,
376 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
379 { USB_VENDOR_CASIO, USB_PRODUCT_CASIO_QV_DIGICAM, RID_WILDCARD,
380 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
383 { USB_VENDOR_CCYU, USB_PRODUCT_CCYU_ED1064, RID_WILDCARD,
384 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
387 { USB_VENDOR_CENTURY, USB_PRODUCT_CENTURY_EX35QUAT, RID_WILDCARD,
388 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
389 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
391 { USB_VENDOR_DESKNOTE, USB_PRODUCT_DESKNOTE_UCR_61S2B, RID_WILDCARD,
392 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
395 { USB_VENDOR_DMI, USB_PRODUCT_DMI_CFSM_RW, RID_WILDCARD,
399 { USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_875DC, RID_WILDCARD,
400 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
403 { USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_895, RID_WILDCARD,
404 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
407 { USB_VENDOR_FEIYA, USB_PRODUCT_FEIYA_5IN1, RID_WILDCARD,
408 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
411 { USB_VENDOR_FREECOM, USB_PRODUCT_FREECOM_DVD, RID_WILDCARD,
415 { USB_VENDOR_FUJIPHOTO, USB_PRODUCT_FUJIPHOTO_MASS0100, RID_WILDCARD,
416 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
419 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE, RID_WILDCARD,
420 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
421 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE |
424 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE_2, RID_WILDCARD,
425 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
426 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
428 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB, RID_WILDCARD,
429 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
430 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
432 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB_2, RID_WILDCARD,
433 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
436 { USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FG, RID_WILDCARD,
437 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
440 { USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FGSM, RID_WILDCARD,
441 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
444 { USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_USB, RID_WILDCARD,
445 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
448 { USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_DZ_MV100A, RID_WILDCARD,
449 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
452 { USB_VENDOR_HP, USB_PRODUCT_HP_CDW4E, RID_WILDCARD,
456 { USB_VENDOR_HP, USB_PRODUCT_HP_CDW8200, RID_WILDCARD,
457 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
458 NO_TEST_UNIT_READY | NO_START_STOP
460 { USB_VENDOR_IMAGINATION, USB_PRODUCT_IMAGINATION_DBX1, RID_WILDCARD,
461 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
464 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_ATAPI, RID_WILDCARD,
465 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
468 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_STORAGE_V2, RID_WILDCARD,
469 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
472 { USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_USBCABLE, RID_WILDCARD,
473 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
474 NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
476 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_IU_CD2, RID_WILDCARD,
477 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
480 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_DVR_UEH8, RID_WILDCARD,
481 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
484 { USB_VENDOR_IOMEGA, USB_PRODUCT_IOMEGA_ZIP100, RID_WILDCARD,
485 /* XXX This is not correct as there are Zip drives that use ATAPI.
487 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
490 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_L3, RID_WILDCARD,
491 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
494 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S3X, RID_WILDCARD,
495 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
498 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S4, RID_WILDCARD,
499 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
502 { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S5, RID_WILDCARD,
503 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
506 { USB_VENDOR_LACIE, USB_PRODUCT_LACIE_HD, RID_WILDCARD,
507 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
510 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_CF_READER, RID_WILDCARD,
511 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
514 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_JUMPSHOT, RID_WILDCARD,
518 { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443SU2, RID_WILDCARD,
522 { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443U2, RID_WILDCARD,
523 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
526 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_DUBPXXG, RID_WILDCARD,
527 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
528 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
530 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_DPCM, RID_WILDCARD,
531 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
532 NO_TEST_UNIT_READY | NO_START_STOP
534 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIDB25, RID_WILDCARD,
535 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
538 { USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIHD50, RID_WILDCARD,
539 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
542 { USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_E223, RID_WILDCARD,
546 { USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_F300, RID_WILDCARD,
547 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
550 { USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_CDRRW, RID_WILDCARD,
551 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
554 { USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_FDD, RID_WILDCARD,
555 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
558 { USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_E398, RID_WILDCARD,
559 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
560 FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
562 { USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY, RID_WILDCARD,
563 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
564 IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
566 { USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY2, RID_WILDCARD,
567 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
570 { USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN, RID_WILDCARD,
571 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
572 NO_INQUIRY | IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
574 { USB_VENDOR_MYSON, USB_PRODUCT_MYSON_STARREADER, RID_WILDCARD,
575 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
578 { USB_VENDOR_NEODIO, USB_PRODUCT_NEODIO_ND3260, RID_WILDCARD,
579 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
582 { USB_VENDOR_NETAC, USB_PRODUCT_NETAC_CF_CARD, RID_WILDCARD,
583 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
586 { USB_VENDOR_NETAC, USB_PRODUCT_NETAC_ONLYDISK, RID_WILDCARD,
587 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
590 { USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_CLIK_40, RID_WILDCARD,
594 { USB_VENDOR_NIKON, USB_PRODUCT_NIKON_D300, RID_WILDCARD,
595 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
598 { USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C1, RID_WILDCARD,
599 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
602 { USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C700, RID_WILDCARD,
606 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFMS_RW, RID_WILDCARD,
610 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_COMBO, RID_WILDCARD,
614 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER, RID_WILDCARD,
618 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER2, RID_WILDCARD,
622 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_SDS_HOTFIND_D, RID_WILDCARD,
623 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
624 NO_GETMAXLUN | NO_SYNCHRONIZE_CACHE
626 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDCFE_B_CF_READER, RID_WILDCARD,
630 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDSM_B_READER, RID_WILDCARD,
634 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_READER, RID_WILDCARD,
638 { USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_UCF100, RID_WILDCARD,
639 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
640 NO_INQUIRY | NO_GETMAXLUN
642 { USB_VENDOR_ONSPEC2, USB_PRODUCT_ONSPEC2_IMAGEMATE_SDDR55, RID_WILDCARD,
646 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXL840AN, RID_WILDCARD,
647 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
650 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB20AN, RID_WILDCARD,
651 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
654 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB35AN, RID_WILDCARD,
655 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
658 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_LS120CAM, RID_WILDCARD,
662 { USB_VENDOR_PLEXTOR, USB_PRODUCT_PLEXTOR_40_12_40U, RID_WILDCARD,
663 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
666 { USB_VENDOR_PNY, USB_PRODUCT_PNY_ATTACHE2, RID_WILDCARD,
667 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
668 IGNORE_RESIDUE | NO_START_STOP
670 { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_YP_U2, RID_WILDCARD,
671 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
672 SHUTTLE_INIT | NO_GETMAXLUN
674 { USB_VENDOR_SAMSUNG_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
675 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
678 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
679 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
680 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
682 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
684 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
686 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
687 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
688 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
690 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
691 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
694 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
695 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
698 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
699 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
702 { USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
703 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
706 { USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
707 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
710 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
711 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
714 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
715 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
718 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
719 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
720 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
722 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
723 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
726 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
730 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
731 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
734 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
735 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
738 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
742 { USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
743 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
746 { USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
747 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
750 { USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
751 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
754 { USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
755 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
758 { USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
759 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
762 { USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
763 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
766 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
767 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
770 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
771 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
774 { USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
775 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
778 { USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, 0x0500,
779 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
782 { USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
783 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
786 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
787 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
790 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
791 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
794 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
795 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
798 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
799 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
802 { USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
803 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
806 { USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
807 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
810 { USB_VENDOR_SUPERTOP, USB_PRODUCT_SUPERTOP_IDE, RID_WILDCARD,
811 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
812 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
814 { USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
818 { USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
819 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
822 { USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
823 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
826 { USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
827 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
830 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
831 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
834 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
838 { USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
842 { USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
843 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
846 { USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
847 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
850 { USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
851 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
854 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
855 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
856 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
858 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, RID_WILDCARD,
859 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
860 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
862 { USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYBOOK, RID_WILDCARD,
863 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
866 { USB_VENDOR_WINMAXGROUP, USB_PRODUCT_WINMAXGROUP_FLASH64MC, RID_WILDCARD,
867 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
870 { USB_VENDOR_YANO, USB_PRODUCT_YANO_FW800HD, RID_WILDCARD,
871 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
872 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
874 { USB_VENDOR_YANO, USB_PRODUCT_YANO_U640MO, RID_WILDCARD,
875 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
878 { USB_VENDOR_YEDATA, USB_PRODUCT_YEDATA_FLASHBUSTERU, RID_WILDCARD,
879 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
882 { USB_VENDOR_ZORAN, USB_PRODUCT_ZORAN_EX20DSC, RID_WILDCARD,
883 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
886 { USB_VENDOR_MEIZU, USB_PRODUCT_MEIZU_M6_SL, RID_WILDCARD,
887 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
888 NO_INQUIRY | NO_SYNCHRONIZE_CACHE
890 { VID_EOT, PID_EOT, RID_EOT, 0, 0 }
894 /* the per device structure */
896 device_t sc_dev; /* base device */
897 usbd_device_handle sc_udev; /* USB device */
899 struct cam_sim *umass_sim; /* SCSI Interface Module */
901 unsigned char flags; /* various device flags */
902 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
904 u_int16_t proto; /* wire and cmd protocol */
905 u_int16_t quirks; /* they got it almost right */
907 usbd_interface_handle iface; /* Mass Storage interface */
908 int ifaceno; /* MS iface number */
910 u_int8_t bulkin; /* bulk-in Endpoint Address */
911 u_int8_t bulkout; /* bulk-out Endpoint Address */
912 u_int8_t intrin; /* intr-in Endp. (CBI) */
913 usbd_pipe_handle bulkin_pipe;
914 usbd_pipe_handle bulkout_pipe;
915 usbd_pipe_handle intrin_pipe;
917 /* Reset the device in a wire protocol specific way */
920 /* The start of a wire transfer. It prepares the whole transfer (cmd,
921 * data, and status stage) and initiates it. It is up to the state
922 * machine (below) to handle the various stages and errors in these
924 wire_transfer_f transfer;
926 /* The state machine, handling the various states during a transfer */
929 /* The command transform function is used to conver the SCSI commands
930 * into their derivatives, like UFI, ATAPI, and friends.
932 command_transform_f transform; /* command transform */
934 /* Bulk specific variables for transfers in progress */
935 umass_bbb_cbw_t cbw; /* command block wrapper */
936 umass_bbb_csw_t csw; /* command status wrapper*/
937 /* CBI specific variables for transfers in progress */
938 umass_cbi_cbl_t cbl; /* command block */
939 umass_cbi_sbl_t sbl; /* status block */
941 /* generic variables for transfers in progress */
942 /* ctrl transfer requests */
943 usb_device_request_t request;
946 * Most of our operations are initiated from interrupt context, so
947 * we need to avoid using the one that is in use. We want to avoid
948 * allocating them in the interrupt context as well.
950 /* indices into array below */
951 # define XFER_BBB_CBW 0 /* Bulk-Only */
952 # define XFER_BBB_DATA 1
953 # define XFER_BBB_DCLEAR 2
954 # define XFER_BBB_CSW1 3
955 # define XFER_BBB_CSW2 4
956 # define XFER_BBB_SCLEAR 5
957 # define XFER_BBB_RESET1 6
958 # define XFER_BBB_RESET2 7
959 # define XFER_BBB_RESET3 8
961 # define XFER_CBI_CB 0 /* CBI */
962 # define XFER_CBI_DATA 1
963 # define XFER_CBI_STATUS 2
964 # define XFER_CBI_DCLEAR 3
965 # define XFER_CBI_SCLEAR 4
966 # define XFER_CBI_RESET1 5
967 # define XFER_CBI_RESET2 6
968 # define XFER_CBI_RESET3 7
970 # define XFER_NR 9 /* maximum number */
972 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
974 int transfer_dir; /* data direction */
975 void *transfer_data; /* data buffer */
976 int transfer_datalen; /* (maximum) length */
977 int transfer_actlen; /* actual length */
978 transfer_cb_f transfer_cb; /* callback */
979 void *transfer_priv; /* for callback */
983 # define TSTATE_ATTACH 0 /* in attach */
984 # define TSTATE_IDLE 1
985 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
986 # define TSTATE_BBB_DATA 3 /* Data transfer */
987 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
988 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
989 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
990 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
991 # define TSTATE_BBB_RESET1 8 /* reset command */
992 # define TSTATE_BBB_RESET2 9 /* in clear stall */
993 # define TSTATE_BBB_RESET3 10 /* out clear stall */
994 # define TSTATE_CBI_COMMAND 11 /* command transfer */
995 # define TSTATE_CBI_DATA 12 /* data transfer */
996 # define TSTATE_CBI_STATUS 13 /* status transfer */
997 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
998 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
999 # define TSTATE_CBI_RESET1 16 /* reset command */
1000 # define TSTATE_CBI_RESET2 17 /* in clear stall */
1001 # define TSTATE_CBI_RESET3 18 /* out clear stall */
1002 # define TSTATE_STATES 19 /* # of states above */
1005 /* SCSI/CAM specific variables */
1006 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
1007 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
1008 struct scsi_sense cam_scsi_sense;
1009 struct scsi_sense cam_scsi_test_unit_ready;
1010 struct callout cam_scsi_rescan_ch;
1012 int timeout; /* in msecs */
1014 int maxlun; /* maximum LUN number */
1018 char *states[TSTATE_STATES+1] = {
1019 /* should be kept in sync with the list at transfer_state */
1024 "BBB Data bulk-in/-out clear stall",
1025 "BBB CSW, 1st attempt",
1026 "BBB CSW bulk-in clear stall",
1027 "BBB CSW, 2nd attempt",
1029 "BBB bulk-in clear stall",
1030 "BBB bulk-out clear stall",
1034 "CBI Data bulk-in/-out clear stall",
1035 "CBI Status intr-in clear stall",
1037 "CBI bulk-in clear stall",
1038 "CBI bulk-out clear stall",
1043 /* If device cannot return valid inquiry data, fake it */
1044 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1045 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
1046 /*additional_length*/ 31, 0, 0, 0
1049 /* USB device probe/attach/detach functions */
1050 static device_probe_t umass_match;
1051 static device_attach_t umass_attach;
1052 static device_detach_t umass_detach;
1054 static device_method_t umass_methods[] = {
1055 /* Device interface */
1056 DEVMETHOD(device_probe, umass_match),
1057 DEVMETHOD(device_attach, umass_attach),
1058 DEVMETHOD(device_detach, umass_detach),
1063 static driver_t umass_driver = {
1066 sizeof(struct umass_softc)
1069 static devclass_t umass_devclass;
1071 static int umass_match_proto (struct umass_softc *sc,
1072 usbd_interface_handle iface,
1073 usbd_device_handle udev);
1075 /* quirk functions */
1076 static void umass_init_shuttle (struct umass_softc *sc);
1078 /* generic transfer functions */
1079 static usbd_status umass_setup_transfer (struct umass_softc *sc,
1080 usbd_pipe_handle pipe,
1081 void *buffer, int buflen, int flags,
1082 usbd_xfer_handle xfer);
1083 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
1084 usbd_device_handle udev,
1085 usb_device_request_t *req,
1086 void *buffer, int buflen, int flags,
1087 usbd_xfer_handle xfer);
1088 static void umass_clear_endpoint_stall (struct umass_softc *sc,
1089 u_int8_t endpt, usbd_pipe_handle pipe,
1090 int state, usbd_xfer_handle xfer);
1091 static void umass_reset (struct umass_softc *sc,
1092 transfer_cb_f cb, void *priv);
1094 /* Bulk-Only related functions */
1095 static void umass_bbb_reset (struct umass_softc *sc, int status);
1096 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
1097 void *cmd, int cmdlen,
1098 void *data, int datalen, int dir, u_int timeout,
1099 transfer_cb_f cb, void *priv);
1100 static void umass_bbb_state (usbd_xfer_handle xfer,
1101 usbd_private_handle priv,
1103 static int umass_bbb_get_max_lun
1104 (struct umass_softc *sc);
1106 /* CBI related functions */
1107 static int umass_cbi_adsc (struct umass_softc *sc,
1108 char *buffer, int buflen,
1109 usbd_xfer_handle xfer);
1110 static void umass_cbi_reset (struct umass_softc *sc, int status);
1111 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
1112 void *cmd, int cmdlen,
1113 void *data, int datalen, int dir, u_int timeout,
1114 transfer_cb_f cb, void *priv);
1115 static void umass_cbi_state (usbd_xfer_handle xfer,
1116 usbd_private_handle priv, usbd_status err);
1118 /* CAM related functions */
1119 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
1120 static void umass_cam_poll (struct cam_sim *sim);
1122 static void umass_cam_cb (struct umass_softc *sc, void *priv,
1123 int residue, int status);
1124 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
1125 int residue, int status);
1126 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
1127 int residue, int status);
1129 static void umass_cam_rescan_callback
1130 (struct cam_periph *periph,union ccb *ccb);
1131 static void umass_cam_rescan (void *addr);
1133 static int umass_cam_attach_sim (struct umass_softc *sc);
1134 static int umass_cam_attach (struct umass_softc *sc);
1135 static int umass_cam_detach_sim (struct umass_softc *sc);
1138 /* SCSI specific functions */
1139 static int umass_scsi_transform (struct umass_softc *sc,
1140 unsigned char *cmd, int cmdlen,
1141 unsigned char **rcmd, int *rcmdlen);
1143 /* UFI specific functions */
1144 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1145 static int umass_ufi_transform (struct umass_softc *sc,
1146 unsigned char *cmd, int cmdlen,
1147 unsigned char **rcmd, int *rcmdlen);
1149 /* ATAPI (8070i) specific functions */
1150 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1151 static int umass_atapi_transform (struct umass_softc *sc,
1152 unsigned char *cmd, int cmdlen,
1153 unsigned char **rcmd, int *rcmdlen);
1155 /* RBC specific functions */
1156 static int umass_rbc_transform (struct umass_softc *sc,
1157 unsigned char *cmd, int cmdlen,
1158 unsigned char **rcmd, int *rcmdlen);
1161 /* General debugging functions */
1162 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
1163 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
1164 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
1165 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
1166 int buflen, int printlen);
1169 MODULE_DEPEND(umass, cam, 1, 1, 1);
1170 MODULE_DEPEND(umass, usb, 1, 1, 1);
1173 * USB device probe/attach/detach
1177 * Match the device we are seeing with the devices supported. Fill in the
1178 * description in the softc accordingly. This function is called from both
1183 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
1184 usbd_device_handle udev)
1186 usb_device_descriptor_t *dd;
1187 usb_interface_descriptor_t *id;
1195 dd = usbd_get_device_descriptor(udev);
1197 /* An entry specifically for Y-E Data devices as they don't fit in the
1198 * device description table.
1200 if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA
1201 && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) {
1203 /* Revisions < 1.28 do not handle the interrupt endpoint
1206 if (UGETW(dd->bcdDevice) < 0x128) {
1207 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1209 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1213 * Revisions < 1.28 do not have the TEST UNIT READY command
1214 * Revisions == 1.28 have a broken TEST UNIT READY
1216 if (UGETW(dd->bcdDevice) <= 0x128)
1217 sc->quirks |= NO_TEST_UNIT_READY;
1219 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1220 return(UMATCH_VENDOR_PRODUCT);
1223 /* Check the list of supported devices for a match. While looking,
1224 * check for wildcarded and fully matched. First match wins.
1226 for (i = 0; umass_devdescrs[i].vid != VID_EOT && !found; i++) {
1227 if (umass_devdescrs[i].vid == VID_WILDCARD &&
1228 umass_devdescrs[i].pid == PID_WILDCARD &&
1229 umass_devdescrs[i].rid == RID_WILDCARD) {
1230 printf("umass: ignoring invalid wildcard quirk\n");
1233 if ((umass_devdescrs[i].vid == UGETW(dd->idVendor) ||
1234 umass_devdescrs[i].vid == VID_WILDCARD)
1235 && (umass_devdescrs[i].pid == UGETW(dd->idProduct) ||
1236 umass_devdescrs[i].pid == PID_WILDCARD)) {
1237 if (umass_devdescrs[i].rid == RID_WILDCARD) {
1238 sc->proto = umass_devdescrs[i].proto;
1239 sc->quirks = umass_devdescrs[i].quirks;
1240 return (UMATCH_VENDOR_PRODUCT);
1241 } else if (umass_devdescrs[i].rid ==
1242 UGETW(dd->bcdDevice)) {
1243 sc->proto = umass_devdescrs[i].proto;
1244 sc->quirks = umass_devdescrs[i].quirks;
1245 return (UMATCH_VENDOR_PRODUCT_REV);
1246 } /* else RID does not match */
1250 /* Check for a standards compliant device */
1251 id = usbd_get_interface_descriptor(iface);
1252 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
1253 return(UMATCH_NONE);
1255 switch (id->bInterfaceSubClass) {
1256 case UISUBCLASS_SCSI:
1257 sc->proto |= UMASS_PROTO_SCSI;
1259 case UISUBCLASS_UFI:
1260 sc->proto |= UMASS_PROTO_UFI;
1262 case UISUBCLASS_RBC:
1263 sc->proto |= UMASS_PROTO_RBC;
1265 case UISUBCLASS_SFF8020I:
1266 case UISUBCLASS_SFF8070I:
1267 sc->proto |= UMASS_PROTO_ATAPI;
1270 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
1271 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
1272 return(UMATCH_NONE);
1275 switch (id->bInterfaceProtocol) {
1276 case UIPROTO_MASS_CBI:
1277 sc->proto |= UMASS_PROTO_CBI;
1279 case UIPROTO_MASS_CBI_I:
1280 sc->proto |= UMASS_PROTO_CBI_I;
1282 case UIPROTO_MASS_BBB_OLD:
1283 case UIPROTO_MASS_BBB:
1284 sc->proto |= UMASS_PROTO_BBB;
1287 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
1288 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
1289 return(UMATCH_NONE);
1292 return(UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
1296 umass_match(device_t self)
1298 struct usb_attach_arg *uaa = device_get_ivars(self);
1299 struct umass_softc *sc = device_get_softc(self);
1302 if (uaa->iface == NULL)
1303 return(UMATCH_NONE);
1305 return(umass_match_proto(sc, uaa->iface, uaa->device));
1309 umass_attach(device_t self)
1311 struct umass_softc *sc = device_get_softc(self);
1312 struct usb_attach_arg *uaa = device_get_ivars(self);
1313 usb_interface_descriptor_t *id;
1314 usb_endpoint_descriptor_t *ed;
1319 * the softc struct is bzero-ed in device_set_driver. We can safely
1320 * call umass_detach without specifically initialising the struct.
1323 sc->iface = uaa->iface;
1324 sc->ifaceno = uaa->ifaceno;
1325 callout_init(&sc->cam_scsi_rescan_ch, 0);
1327 /* initialise the proto and drive values in the umass_softc (again) */
1328 (void) umass_match_proto(sc, sc->iface, uaa->device);
1330 id = usbd_get_interface_descriptor(sc->iface);
1332 printf("%s: ", device_get_nameunit(sc->sc_dev));
1333 switch (sc->proto&UMASS_PROTO_COMMAND) {
1334 case UMASS_PROTO_SCSI:
1337 case UMASS_PROTO_ATAPI:
1338 printf("8070i (ATAPI)");
1340 case UMASS_PROTO_UFI:
1343 case UMASS_PROTO_RBC:
1347 printf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
1351 switch (sc->proto&UMASS_PROTO_WIRE) {
1352 case UMASS_PROTO_BBB:
1353 printf("Bulk-Only");
1355 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1358 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1359 printf("CBI with CCI");
1361 printf(" (using CBI)");
1365 printf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
1367 printf("; quirks = 0x%04x\n", sc->quirks);
1371 if (sc->proto & UMASS_PROTO_CBI_I) {
1372 /* See beginning of file for comment on the use of CBI with CCI */
1373 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
1377 if (sc->quirks & ALT_IFACE_1) {
1378 err = usbd_set_interface(uaa->iface, 1);
1380 DPRINTF(UDMASS_USB, ("%s: could not switch to "
1381 "Alt Interface %d\n",
1382 device_get_nameunit(sc->sc_dev), 1));
1389 * In addition to the Control endpoint the following endpoints
1391 * a) bulk-in endpoint.
1392 * b) bulk-out endpoint.
1393 * and for Control/Bulk/Interrupt with CCI (CBI_I)
1396 * The endpoint addresses are not fixed, so we have to read them
1397 * from the device descriptors of the current interface.
1399 for (i = 0 ; i < id->bNumEndpoints ; i++) {
1400 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
1402 printf("%s: could not read endpoint descriptor\n",
1403 device_get_nameunit(sc->sc_dev));
1406 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1407 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1408 sc->bulkin = ed->bEndpointAddress;
1409 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
1410 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1411 sc->bulkout = ed->bEndpointAddress;
1412 } else if (sc->proto & UMASS_PROTO_CBI_I
1413 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1414 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1415 sc->intrin = ed->bEndpointAddress;
1417 if (UGETW(ed->wMaxPacketSize) > 2) {
1418 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
1419 device_get_nameunit(sc->sc_dev),
1420 UGETW(ed->wMaxPacketSize)));
1426 /* check whether we found all the endpoints we need */
1427 if (!sc->bulkin || !sc->bulkout
1428 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
1429 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
1430 device_get_nameunit(sc->sc_dev),
1431 sc->bulkin, sc->bulkout, sc->intrin));
1436 /* Open the bulk-in and -out pipe */
1437 err = usbd_open_pipe(sc->iface, sc->bulkout,
1438 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
1440 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
1441 device_get_nameunit(sc->sc_dev), sc->bulkout));
1445 err = usbd_open_pipe(sc->iface, sc->bulkin,
1446 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
1448 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
1449 device_get_nameunit(sc->sc_dev), sc->bulkin));
1453 /* Open the intr-in pipe if the protocol is CBI with CCI.
1454 * Note: early versions of the Zip drive do have an interrupt pipe, but
1455 * this pipe is unused.
1457 * We do not open the interrupt pipe as an interrupt pipe, but as a
1458 * normal bulk endpoint. We send an IN transfer down the wire at the
1459 * appropriate time, because we know exactly when to expect data on
1460 * that endpoint. This saves bandwidth, but more important, makes the
1461 * code for handling the data on that endpoint simpler. No data
1462 * arriving concurrently.
1464 if (sc->proto & UMASS_PROTO_CBI_I) {
1465 err = usbd_open_pipe(sc->iface, sc->intrin,
1466 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1468 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1469 device_get_nameunit(sc->sc_dev), sc->intrin));
1475 /* initialisation of generic part */
1476 sc->transfer_state = TSTATE_ATTACH;
1478 /* request a sufficient number of xfer handles */
1479 for (i = 0; i < XFER_NR; i++) {
1480 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1481 if (!sc->transfer_xfer[i]) {
1482 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1483 device_get_nameunit(sc->sc_dev)));
1489 /* Initialise the wire protocol specific methods */
1490 if (sc->proto & UMASS_PROTO_BBB) {
1491 sc->reset = umass_bbb_reset;
1492 sc->transfer = umass_bbb_transfer;
1493 sc->state = umass_bbb_state;
1494 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1495 sc->reset = umass_cbi_reset;
1496 sc->transfer = umass_cbi_transfer;
1497 sc->state = umass_cbi_state;
1500 panic("%s:%d: Unknown proto 0x%02x",
1501 __FILE__, __LINE__, sc->proto);
1505 if (sc->proto & UMASS_PROTO_SCSI)
1506 sc->transform = umass_scsi_transform;
1507 else if (sc->proto & UMASS_PROTO_UFI)
1508 sc->transform = umass_ufi_transform;
1509 else if (sc->proto & UMASS_PROTO_ATAPI)
1510 sc->transform = umass_atapi_transform;
1511 else if (sc->proto & UMASS_PROTO_RBC)
1512 sc->transform = umass_rbc_transform;
1515 panic("No transformation defined for command proto 0x%02x",
1516 sc->proto & UMASS_PROTO_COMMAND);
1519 /* From here onwards the device can be used. */
1521 if (sc->quirks & SHUTTLE_INIT)
1522 umass_init_shuttle(sc);
1524 /* Get the maximum LUN supported by the device.
1526 if (((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1527 !(sc->quirks & NO_GETMAXLUN))
1528 sc->maxlun = umass_bbb_get_max_lun(sc);
1532 if ((sc->proto & UMASS_PROTO_SCSI) ||
1533 (sc->proto & UMASS_PROTO_ATAPI) ||
1534 (sc->proto & UMASS_PROTO_UFI) ||
1535 (sc->proto & UMASS_PROTO_RBC)) {
1536 /* Prepare the SCSI command block */
1537 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1538 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1540 /* register the SIM */
1541 err = umass_cam_attach_sim(sc);
1546 /* scan the new sim */
1547 err = umass_cam_attach(sc);
1549 umass_cam_detach_sim(sc);
1554 panic("%s:%d: Unknown proto 0x%02x",
1555 __FILE__, __LINE__, sc->proto);
1558 sc->transfer_state = TSTATE_IDLE;
1559 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1565 umass_detach(device_t self)
1567 struct umass_softc *sc = device_get_softc(self);
1571 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1573 sc->flags |= UMASS_FLAGS_GONE;
1575 /* abort all the pipes in case there are transfers active. */
1576 usbd_abort_default_pipe(sc->sc_udev);
1577 if (sc->bulkout_pipe)
1578 usbd_abort_pipe(sc->bulkout_pipe);
1579 if (sc->bulkin_pipe)
1580 usbd_abort_pipe(sc->bulkin_pipe);
1581 if (sc->intrin_pipe)
1582 usbd_abort_pipe(sc->intrin_pipe);
1584 callout_drain(&sc->cam_scsi_rescan_ch);
1585 if ((sc->proto & UMASS_PROTO_SCSI) ||
1586 (sc->proto & UMASS_PROTO_ATAPI) ||
1587 (sc->proto & UMASS_PROTO_UFI) ||
1588 (sc->proto & UMASS_PROTO_RBC))
1589 /* detach the SCSI host controller (SIM) */
1590 err = umass_cam_detach_sim(sc);
1592 for (i = 0; i < XFER_NR; i++)
1593 if (sc->transfer_xfer[i])
1594 usbd_free_xfer(sc->transfer_xfer[i]);
1596 /* remove all the pipes */
1597 if (sc->bulkout_pipe)
1598 usbd_close_pipe(sc->bulkout_pipe);
1599 if (sc->bulkin_pipe)
1600 usbd_close_pipe(sc->bulkin_pipe);
1601 if (sc->intrin_pipe)
1602 usbd_close_pipe(sc->intrin_pipe);
1608 umass_init_shuttle(struct umass_softc *sc)
1610 usb_device_request_t req;
1613 /* The Linux driver does this, but no one can tell us what the
1616 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1617 req.bRequest = 1; /* XXX unknown command */
1618 USETW(req.wValue, 0);
1619 USETW(req.wIndex, sc->ifaceno);
1620 USETW(req.wLength, sizeof status);
1621 (void) usbd_do_request(sc->sc_udev, &req, &status);
1623 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1624 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1628 * Generic functions to handle transfers
1632 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1633 void *buffer, int buflen, int flags,
1634 usbd_xfer_handle xfer)
1638 /* Initialise a USB transfer and then schedule it */
1640 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1641 sc->timeout, sc->state);
1643 err = usbd_transfer(xfer);
1644 if (err && err != USBD_IN_PROGRESS) {
1645 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1646 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1650 return (USBD_NORMAL_COMPLETION);
1655 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1656 usb_device_request_t *req,
1657 void *buffer, int buflen, int flags,
1658 usbd_xfer_handle xfer)
1662 /* Initialise a USB control transfer and then schedule it */
1664 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1665 sc->timeout, req, buffer, buflen, flags, sc->state);
1667 err = usbd_transfer(xfer);
1668 if (err && err != USBD_IN_PROGRESS) {
1669 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1670 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1672 /* do not reset, as this would make us loop */
1676 return (USBD_NORMAL_COMPLETION);
1680 umass_clear_endpoint_stall(struct umass_softc *sc,
1681 u_int8_t endpt, usbd_pipe_handle pipe,
1682 int state, usbd_xfer_handle xfer)
1684 usbd_device_handle udev;
1686 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1687 device_get_nameunit(sc->sc_dev), endpt));
1689 usbd_interface2device_handle(sc->iface, &udev);
1691 sc->transfer_state = state;
1693 usbd_clear_endpoint_toggle(pipe);
1695 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1696 sc->request.bRequest = UR_CLEAR_FEATURE;
1697 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1698 USETW(sc->request.wIndex, endpt);
1699 USETW(sc->request.wLength, 0);
1700 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1704 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1706 sc->transfer_cb = cb;
1707 sc->transfer_priv = priv;
1709 /* The reset is a forced reset, so no error (yet) */
1710 sc->reset(sc, STATUS_CMD_OK);
1714 * Bulk protocol specific functions
1718 umass_bbb_reset(struct umass_softc *sc, int status)
1720 usbd_device_handle udev;
1722 KASSERT(sc->proto & UMASS_PROTO_BBB,
1723 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x\n",
1724 device_get_nameunit(sc->sc_dev), sc->proto));
1727 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1729 * For Reset Recovery the host shall issue in the following order:
1730 * a) a Bulk-Only Mass Storage Reset
1731 * b) a Clear Feature HALT to the Bulk-In endpoint
1732 * c) a Clear Feature HALT to the Bulk-Out endpoint
1734 * This is done in 3 steps, states:
1739 * If the reset doesn't succeed, the device should be port reset.
1742 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1743 device_get_nameunit(sc->sc_dev)));
1745 sc->transfer_state = TSTATE_BBB_RESET1;
1746 sc->transfer_status = status;
1748 usbd_interface2device_handle(sc->iface, &udev);
1750 /* reset is a class specific interface write */
1751 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1752 sc->request.bRequest = UR_BBB_RESET;
1753 USETW(sc->request.wValue, 0);
1754 USETW(sc->request.wIndex, sc->ifaceno);
1755 USETW(sc->request.wLength, 0);
1756 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1757 sc->transfer_xfer[XFER_BBB_RESET1]);
1761 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1762 void *data, int datalen, int dir, u_int timeout,
1763 transfer_cb_f cb, void *priv)
1765 KASSERT(sc->proto & UMASS_PROTO_BBB,
1766 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x\n",
1767 device_get_nameunit(sc->sc_dev), sc->proto));
1769 /* Be a little generous. */
1770 sc->timeout = timeout + UMASS_TIMEOUT;
1773 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1774 * a data phase of datalen bytes from/to the device and finally a
1776 * If the data direction was inbound a maximum of datalen bytes
1777 * is stored in the buffer pointed to by data.
1779 * umass_bbb_transfer initialises the transfer and lets the state
1780 * machine in umass_bbb_state handle the completion. It uses the
1782 * TSTATE_BBB_COMMAND
1783 * -> TSTATE_BBB_DATA
1784 * -> TSTATE_BBB_STATUS
1785 * -> TSTATE_BBB_STATUS2
1786 * -> TSTATE_BBB_IDLE
1788 * An error in any of those states will invoke
1792 /* check the given arguments */
1793 KASSERT(datalen == 0 || data != NULL,
1794 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1795 KASSERT(cmdlen <= CBWCDBLENGTH,
1796 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1797 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1798 KASSERT(dir == DIR_NONE || datalen > 0,
1799 ("%s: datalen == 0 while direction is not NONE\n",
1800 device_get_nameunit(sc->sc_dev)));
1801 KASSERT(datalen == 0 || dir != DIR_NONE,
1802 ("%s: direction is NONE while datalen is not zero\n",
1803 device_get_nameunit(sc->sc_dev)));
1804 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1805 ("%s: CBW struct does not have the right size (%ld vs. %d)\n",
1806 device_get_nameunit(sc->sc_dev),
1807 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1808 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1809 ("%s: CSW struct does not have the right size (%ld vs. %d)\n",
1810 device_get_nameunit(sc->sc_dev),
1811 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1814 * Determine the direction of the data transfer and the length.
1816 * dCBWDataTransferLength (datalen) :
1817 * This field indicates the number of bytes of data that the host
1818 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1819 * the Direction bit) during the execution of this command. If this
1820 * field is set to 0, the device will expect that no data will be
1821 * transferred IN or OUT during this command, regardless of the value
1822 * of the Direction bit defined in dCBWFlags.
1825 * The bits of the Flags field are defined as follows:
1827 * Bit 7 Direction - this bit shall be ignored if the
1828 * dCBWDataTransferLength field is zero.
1829 * 0 = data Out from host to device
1830 * 1 = data In from device to host
1833 /* Fill in the Command Block Wrapper
1834 * We fill in all the fields, so there is no need to bzero it first.
1836 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1837 /* We don't care about the initial value, as long as the values are unique */
1838 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1839 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1840 /* DIR_NONE is treated as DIR_OUT (0x00) */
1841 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1842 sc->cbw.bCBWLUN = lun;
1843 sc->cbw.bCDBLength = cmdlen;
1844 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1846 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1848 /* store the details for the data transfer phase */
1849 sc->transfer_dir = dir;
1850 sc->transfer_data = data;
1851 sc->transfer_datalen = datalen;
1852 sc->transfer_actlen = 0;
1853 sc->transfer_cb = cb;
1854 sc->transfer_priv = priv;
1855 sc->transfer_status = STATUS_CMD_OK;
1857 /* move from idle to the command state */
1858 sc->transfer_state = TSTATE_BBB_COMMAND;
1860 /* Send the CBW from host to device via bulk-out endpoint. */
1861 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1862 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1863 sc->transfer_xfer[XFER_BBB_CBW])) {
1864 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1870 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1873 struct umass_softc *sc = (struct umass_softc *) priv;
1874 usbd_xfer_handle next_xfer;
1877 KASSERT(sc->proto & UMASS_PROTO_BBB,
1878 ("%s: umass_bbb_state: wrong sc->proto 0x%02x\n",
1879 device_get_nameunit(sc->sc_dev), sc->proto));
1882 * State handling for BBB transfers.
1884 * The subroutine is rather long. It steps through the states given in
1885 * Annex A of the Bulk-Only specification.
1886 * Each state first does the error handling of the previous transfer
1887 * and then prepares the next transfer.
1888 * Each transfer is done asynchronously so after the request/transfer
1889 * has been submitted you will find a 'return;'.
1892 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1893 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1894 states[sc->transfer_state], xfer, usbd_errstr(err)));
1896 /* Give up if the device has detached. */
1897 if (sc->flags & UMASS_FLAGS_GONE) {
1898 sc->transfer_state = TSTATE_IDLE;
1899 sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
1904 switch (sc->transfer_state) {
1906 /***** Bulk Transfer *****/
1907 case TSTATE_BBB_COMMAND:
1908 /* Command transport phase, error handling */
1910 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1911 device_get_nameunit(sc->sc_dev)));
1912 /* If the device detects that the CBW is invalid, then
1913 * the device may STALL both bulk endpoints and require
1916 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1920 /* Data transport phase, setup transfer */
1921 sc->transfer_state = TSTATE_BBB_DATA;
1922 if (sc->transfer_dir == DIR_IN) {
1923 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1924 sc->transfer_data, sc->transfer_datalen,
1926 sc->transfer_xfer[XFER_BBB_DATA]))
1927 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1930 } else if (sc->transfer_dir == DIR_OUT) {
1931 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1932 sc->transfer_data, sc->transfer_datalen,
1933 0, /* fixed length transfer */
1934 sc->transfer_xfer[XFER_BBB_DATA]))
1935 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1939 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1940 device_get_nameunit(sc->sc_dev)));
1943 /* FALLTHROUGH if no data phase, err == 0 */
1944 case TSTATE_BBB_DATA:
1945 /* Command transport phase, error handling (ignored if no data
1946 * phase (fallthrough from previous state)) */
1947 if (sc->transfer_dir != DIR_NONE) {
1948 /* retrieve the length of the transfer that was done */
1949 usbd_get_xfer_status(xfer, NULL, NULL,
1950 &sc->transfer_actlen, NULL);
1953 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
1954 "%s\n", device_get_nameunit(sc->sc_dev),
1955 (sc->transfer_dir == DIR_IN?"in":"out"),
1956 sc->transfer_datalen,usbd_errstr(err)));
1958 if (err == USBD_STALLED) {
1959 umass_clear_endpoint_stall(sc,
1960 (sc->transfer_dir == DIR_IN?
1961 sc->bulkin:sc->bulkout),
1962 (sc->transfer_dir == DIR_IN?
1963 sc->bulkin_pipe:sc->bulkout_pipe),
1965 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1968 /* Unless the error is a pipe stall the
1971 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1977 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1978 umass_dump_buffer(sc, sc->transfer_data,
1979 sc->transfer_datalen, 48));
1983 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1984 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1985 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1986 /* Reading of CSW after bulk stall condition in data phase
1987 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1988 * reading CSW (TSTATE_BBB_SCLEAR).
1989 * In the case of no data phase or successfull data phase,
1990 * err == 0 and the following if block is passed.
1992 if (err) { /* should not occur */
1993 /* try the transfer below, even if clear stall failed */
1994 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1995 ", %s\n", device_get_nameunit(sc->sc_dev),
1996 (sc->transfer_dir == DIR_IN? "in":"out"),
1998 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2002 /* Status transport phase, setup transfer */
2003 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
2004 sc->transfer_state == TSTATE_BBB_DATA ||
2005 sc->transfer_state == TSTATE_BBB_DCLEAR) {
2006 /* After no data phase, successfull data phase and
2007 * after clearing bulk-in/-out stall condition
2009 sc->transfer_state = TSTATE_BBB_STATUS1;
2010 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
2012 /* After first attempt of fetching CSW */
2013 sc->transfer_state = TSTATE_BBB_STATUS2;
2014 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
2017 /* Read the Command Status Wrapper via bulk-in endpoint. */
2018 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2019 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
2021 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2026 case TSTATE_BBB_STATUS1: /* first attempt */
2027 case TSTATE_BBB_STATUS2: /* second attempt */
2028 /* Status transfer, error handling */
2030 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
2031 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
2032 (sc->transfer_state == TSTATE_BBB_STATUS1?
2035 /* If this was the first attempt at fetching the CSW
2036 * retry it, otherwise fail.
2038 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
2039 umass_clear_endpoint_stall(sc,
2040 sc->bulkin, sc->bulkin_pipe,
2042 sc->transfer_xfer[XFER_BBB_SCLEAR]);
2045 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2050 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2052 /* Translate weird command-status signatures. */
2053 if (sc->quirks & WRONG_CSWSIG) {
2054 u_int32_t dCSWSignature = UGETDW(sc->csw.dCSWSignature);
2055 if (dCSWSignature == CSWSIGNATURE_OLYMPUS_C1 ||
2056 dCSWSignature == CSWSIGNATURE_IMAGINATION_DBX1)
2057 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2060 Residue = UGETDW(sc->csw.dCSWDataResidue);
2061 if (Residue == 0 || (sc->quirks & IGNORE_RESIDUE))
2062 Residue = sc->transfer_datalen - sc->transfer_actlen;
2064 /* Check CSW and handle any error */
2065 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2066 /* Invalid CSW: Wrong signature or wrong tag might
2067 * indicate that the device is confused -> reset it.
2069 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
2070 device_get_nameunit(sc->sc_dev),
2071 UGETDW(sc->csw.dCSWSignature),
2074 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2076 } else if (UGETDW(sc->csw.dCSWTag)
2077 != UGETDW(sc->cbw.dCBWTag)) {
2078 printf("%s: Invalid CSW: tag %d should be %d\n",
2079 device_get_nameunit(sc->sc_dev),
2080 UGETDW(sc->csw.dCSWTag),
2081 UGETDW(sc->cbw.dCBWTag));
2083 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2086 /* CSW is valid here */
2087 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2088 printf("%s: Invalid CSW: status %d > %d\n",
2089 device_get_nameunit(sc->sc_dev),
2093 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2095 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2096 printf("%s: Phase Error, residue = %d\n",
2097 device_get_nameunit(sc->sc_dev), Residue);
2099 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2102 } else if (sc->transfer_actlen > sc->transfer_datalen) {
2103 /* Buffer overrun! Don't let this go by unnoticed */
2104 panic("%s: transferred %db instead of %db",
2105 device_get_nameunit(sc->sc_dev),
2106 sc->transfer_actlen, sc->transfer_datalen);
2108 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2109 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
2110 device_get_nameunit(sc->sc_dev), Residue));
2112 /* SCSI command failed but transfer was succesful */
2113 sc->transfer_state = TSTATE_IDLE;
2114 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2118 } else { /* success */
2119 sc->transfer_state = TSTATE_IDLE;
2120 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2126 /***** Bulk Reset *****/
2127 case TSTATE_BBB_RESET1:
2129 printf("%s: BBB reset failed, %s\n",
2130 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2132 umass_clear_endpoint_stall(sc,
2133 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
2134 sc->transfer_xfer[XFER_BBB_RESET2]);
2137 case TSTATE_BBB_RESET2:
2138 if (err) /* should not occur */
2139 printf("%s: BBB bulk-in clear stall failed, %s\n",
2140 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2141 /* no error recovery, otherwise we end up in a loop */
2143 umass_clear_endpoint_stall(sc,
2144 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
2145 sc->transfer_xfer[XFER_BBB_RESET3]);
2148 case TSTATE_BBB_RESET3:
2149 if (err) /* should not occur */
2150 printf("%s: BBB bulk-out clear stall failed, %s\n",
2151 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2152 /* no error recovery, otherwise we end up in a loop */
2154 sc->transfer_state = TSTATE_IDLE;
2155 if (sc->transfer_priv) {
2156 sc->transfer_cb(sc, sc->transfer_priv,
2157 sc->transfer_datalen,
2158 sc->transfer_status);
2163 /***** Default *****/
2165 panic("%s: Unknown state %d",
2166 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2171 umass_bbb_get_max_lun(struct umass_softc *sc)
2173 usbd_device_handle udev;
2174 usb_device_request_t req;
2176 usb_interface_descriptor_t *id;
2180 usbd_interface2device_handle(sc->iface, &udev);
2181 id = usbd_get_interface_descriptor(sc->iface);
2183 /* The Get Max Lun command is a class-specific request. */
2184 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2185 req.bRequest = UR_BBB_GET_MAX_LUN;
2186 USETW(req.wValue, 0);
2187 USETW(req.wIndex, id->bInterfaceNumber);
2188 USETW(req.wLength, 1);
2190 err = usbd_do_request(udev, &req, &buf);
2192 case USBD_NORMAL_COMPLETION:
2194 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
2195 device_get_nameunit(sc->sc_dev), maxlun));
2198 case USBD_SHORT_XFER:
2200 /* Device doesn't support Get Max Lun request. */
2201 printf("%s: Get Max Lun not supported (%s)\n",
2202 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2203 /* XXX Should we port_reset the device? */
2211 * Command/Bulk/Interrupt (CBI) specific functions
2215 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
2216 usbd_xfer_handle xfer)
2218 usbd_device_handle udev;
2220 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2221 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x\n",
2222 device_get_nameunit(sc->sc_dev), sc->proto));
2224 usbd_interface2device_handle(sc->iface, &udev);
2226 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2227 sc->request.bRequest = UR_CBI_ADSC;
2228 USETW(sc->request.wValue, 0);
2229 USETW(sc->request.wIndex, sc->ifaceno);
2230 USETW(sc->request.wLength, buflen);
2231 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
2237 umass_cbi_reset(struct umass_softc *sc, int status)
2240 # define SEND_DIAGNOSTIC_CMDLEN 12
2242 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2243 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x\n",
2244 device_get_nameunit(sc->sc_dev), sc->proto));
2247 * Command Block Reset Protocol
2249 * First send a reset request to the device. Then clear
2250 * any possibly stalled bulk endpoints.
2252 * This is done in 3 steps, states:
2257 * If the reset doesn't succeed, the device should be port reset.
2260 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
2261 device_get_nameunit(sc->sc_dev)));
2263 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
2264 ("%s: CBL struct is too small (%ld < %d)\n",
2265 device_get_nameunit(sc->sc_dev),
2266 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
2268 sc->transfer_state = TSTATE_CBI_RESET1;
2269 sc->transfer_status = status;
2271 /* The 0x1d code is the SEND DIAGNOSTIC command. To distinguish between
2272 * the two the last 10 bytes of the cbl is filled with 0xff (section
2273 * 2.2 of the CBI spec).
2275 sc->cbl[0] = 0x1d; /* Command Block Reset */
2277 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
2280 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
2281 sc->transfer_xfer[XFER_CBI_RESET1]);
2282 /* XXX if the command fails we should reset the port on the hub */
2286 umass_cbi_transfer(struct umass_softc *sc, int lun,
2287 void *cmd, int cmdlen, void *data, int datalen, int dir,
2288 u_int timeout, transfer_cb_f cb, void *priv)
2290 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2291 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x\n",
2292 device_get_nameunit(sc->sc_dev), sc->proto));
2294 /* Be a little generous. */
2295 sc->timeout = timeout + UMASS_TIMEOUT;
2298 * Do a CBI transfer with cmdlen bytes from cmd, possibly
2299 * a data phase of datalen bytes from/to the device and finally a
2301 * If the data direction was inbound a maximum of datalen bytes
2302 * is stored in the buffer pointed to by data.
2304 * umass_cbi_transfer initialises the transfer and lets the state
2305 * machine in umass_cbi_state handle the completion. It uses the
2307 * TSTATE_CBI_COMMAND
2310 * An error in any of those states will invoke
2314 /* check the given arguments */
2315 KASSERT(datalen == 0 || data != NULL,
2316 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
2317 KASSERT(datalen == 0 || dir != DIR_NONE,
2318 ("%s: direction is NONE while datalen is not zero\n",
2319 device_get_nameunit(sc->sc_dev)));
2321 /* store the details for the data transfer phase */
2322 sc->transfer_dir = dir;
2323 sc->transfer_data = data;
2324 sc->transfer_datalen = datalen;
2325 sc->transfer_actlen = 0;
2326 sc->transfer_cb = cb;
2327 sc->transfer_priv = priv;
2328 sc->transfer_status = STATUS_CMD_OK;
2330 /* move from idle to the command state */
2331 sc->transfer_state = TSTATE_CBI_COMMAND;
2333 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
2335 /* Send the Command Block from host to device via control endpoint. */
2336 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2337 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2341 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2344 struct umass_softc *sc = (struct umass_softc *) priv;
2346 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2347 ("%s: umass_cbi_state: wrong sc->proto 0x%02x\n",
2348 device_get_nameunit(sc->sc_dev), sc->proto));
2351 * State handling for CBI transfers.
2354 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2355 device_get_nameunit(sc->sc_dev), sc->transfer_state,
2356 states[sc->transfer_state], xfer, usbd_errstr(err)));
2358 /* Give up if the device has detached. */
2359 if (sc->flags & UMASS_FLAGS_GONE) {
2360 sc->transfer_state = TSTATE_IDLE;
2361 sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
2366 switch (sc->transfer_state) {
2368 /***** CBI Transfer *****/
2369 case TSTATE_CBI_COMMAND:
2370 if (err == USBD_STALLED) {
2371 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2372 device_get_nameunit(sc->sc_dev)));
2373 /* Status transport by control pipe (section 2.3.2.1).
2374 * The command contained in the command block failed.
2376 * The control pipe has already been unstalled by the
2378 * Section 2.4.3.1.1 states that the bulk in endpoints
2379 * should not be stalled at this point.
2382 sc->transfer_state = TSTATE_IDLE;
2383 sc->transfer_cb(sc, sc->transfer_priv,
2384 sc->transfer_datalen,
2389 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2390 device_get_nameunit(sc->sc_dev)));
2391 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2396 sc->transfer_state = TSTATE_CBI_DATA;
2397 if (sc->transfer_dir == DIR_IN) {
2398 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2399 sc->transfer_data, sc->transfer_datalen,
2401 sc->transfer_xfer[XFER_CBI_DATA]))
2402 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2404 } else if (sc->transfer_dir == DIR_OUT) {
2405 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2406 sc->transfer_data, sc->transfer_datalen,
2407 0, /* fixed length transfer */
2408 sc->transfer_xfer[XFER_CBI_DATA]))
2409 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2411 } else if (sc->proto & UMASS_PROTO_CBI_I) {
2412 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2413 device_get_nameunit(sc->sc_dev)));
2414 sc->transfer_state = TSTATE_CBI_STATUS;
2415 if (umass_setup_transfer(sc, sc->intrin_pipe,
2416 &sc->sbl, sizeof(sc->sbl),
2417 0, /* fixed length transfer */
2418 sc->transfer_xfer[XFER_CBI_STATUS])){
2419 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2422 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2423 device_get_nameunit(sc->sc_dev)));
2424 /* No command completion interrupt. Request
2427 sc->transfer_state = TSTATE_IDLE;
2428 sc->transfer_cb(sc, sc->transfer_priv,
2429 0, STATUS_CMD_UNKNOWN);
2434 case TSTATE_CBI_DATA:
2435 /* retrieve the length of the transfer that was done */
2436 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2439 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2440 "%s\n", device_get_nameunit(sc->sc_dev),
2441 (sc->transfer_dir == DIR_IN?"in":"out"),
2442 sc->transfer_datalen,usbd_errstr(err)));
2444 if (err == USBD_STALLED) {
2445 umass_clear_endpoint_stall(sc,
2446 sc->bulkin, sc->bulkin_pipe,
2448 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2450 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2455 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2456 umass_dump_buffer(sc, sc->transfer_data,
2457 sc->transfer_actlen, 48));
2459 if (sc->proto & UMASS_PROTO_CBI_I) {
2460 sc->transfer_state = TSTATE_CBI_STATUS;
2461 if (umass_setup_transfer(sc, sc->intrin_pipe,
2462 &sc->sbl, sizeof(sc->sbl),
2463 0, /* fixed length transfer */
2464 sc->transfer_xfer[XFER_CBI_STATUS])){
2465 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2468 /* No command completion interrupt. Request
2469 * sense to get status of command.
2471 sc->transfer_state = TSTATE_IDLE;
2472 sc->transfer_cb(sc, sc->transfer_priv,
2473 sc->transfer_datalen - sc->transfer_actlen,
2474 STATUS_CMD_UNKNOWN);
2478 case TSTATE_CBI_STATUS:
2480 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2481 device_get_nameunit(sc->sc_dev)));
2482 /* Status transport by interrupt pipe (section 2.3.2.2).
2485 if (err == USBD_STALLED) {
2486 umass_clear_endpoint_stall(sc,
2487 sc->intrin, sc->intrin_pipe,
2489 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2491 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2496 /* Dissect the information in the buffer */
2498 if (sc->proto & UMASS_PROTO_UFI) {
2501 /* Section 3.4.3.1.3 specifies that the UFI command
2502 * protocol returns an ASC and ASCQ in the interrupt
2506 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2508 device_get_nameunit(sc->sc_dev),
2509 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2511 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2512 status = STATUS_CMD_OK;
2514 status = STATUS_CMD_FAILED;
2516 sc->transfer_state = TSTATE_IDLE;
2517 sc->transfer_cb(sc, sc->transfer_priv,
2518 sc->transfer_datalen - sc->transfer_actlen,
2521 /* Command Interrupt Data Block */
2522 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2523 device_get_nameunit(sc->sc_dev),
2524 sc->sbl.common.type, sc->sbl.common.value));
2526 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2529 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2530 == IDB_VALUE_PASS) {
2531 err = STATUS_CMD_OK;
2532 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2533 == IDB_VALUE_FAIL ||
2534 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2535 == IDB_VALUE_PERSISTENT) {
2536 err = STATUS_CMD_FAILED;
2538 err = STATUS_WIRE_FAILED;
2541 sc->transfer_state = TSTATE_IDLE;
2542 sc->transfer_cb(sc, sc->transfer_priv,
2543 sc->transfer_datalen-sc->transfer_actlen,
2549 case TSTATE_CBI_DCLEAR:
2550 if (err) { /* should not occur */
2551 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
2552 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2553 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2556 sc->transfer_state = TSTATE_IDLE;
2557 sc->transfer_cb(sc, sc->transfer_priv,
2558 sc->transfer_datalen,
2562 case TSTATE_CBI_SCLEAR:
2563 if (err) /* should not occur */
2564 printf("%s: CBI intr-in stall clear failed, %s\n",
2565 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2567 /* Something really bad is going on. Reset the device */
2568 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2571 /***** CBI Reset *****/
2572 case TSTATE_CBI_RESET1:
2574 printf("%s: CBI reset failed, %s\n",
2575 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2577 umass_clear_endpoint_stall(sc,
2578 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2579 sc->transfer_xfer[XFER_CBI_RESET2]);
2582 case TSTATE_CBI_RESET2:
2583 if (err) /* should not occur */
2584 printf("%s: CBI bulk-in stall clear failed, %s\n",
2585 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2586 /* no error recovery, otherwise we end up in a loop */
2588 umass_clear_endpoint_stall(sc,
2589 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2590 sc->transfer_xfer[XFER_CBI_RESET3]);
2593 case TSTATE_CBI_RESET3:
2594 if (err) /* should not occur */
2595 printf("%s: CBI bulk-out stall clear failed, %s\n",
2596 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2597 /* no error recovery, otherwise we end up in a loop */
2599 sc->transfer_state = TSTATE_IDLE;
2600 if (sc->transfer_priv) {
2601 sc->transfer_cb(sc, sc->transfer_priv,
2602 sc->transfer_datalen,
2603 sc->transfer_status);
2609 /***** Default *****/
2611 panic("%s: Unknown state %d",
2612 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2620 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2624 umass_cam_attach_sim(struct umass_softc *sc)
2626 struct cam_devq *devq; /* Per device Queue */
2628 /* A HBA is attached to the CAM layer.
2630 * The CAM layer will then after a while start probing for
2631 * devices on the bus. The number of SIMs is limited to one.
2634 devq = cam_simq_alloc(1 /*maximum openings*/);
2638 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2641 device_get_unit(sc->sc_dev) /*unit number*/,
2643 1 /*maximum device openings*/,
2644 0 /*maximum tagged device openings*/,
2646 if (sc->umass_sim == NULL) {
2647 cam_simq_free(devq);
2651 if(xpt_bus_register(sc->umass_sim, NULL, device_get_unit(sc->sc_dev)) !=
2659 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2662 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2663 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2664 periph->periph_name, periph->unit_number,
2665 ccb->ccb_h.status));
2667 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2668 periph->periph_name, periph->unit_number));
2672 xpt_free_path(ccb->ccb_h.path);
2673 free(ccb, M_USBDEV);
2677 umass_cam_rescan(void *addr)
2679 struct umass_softc *sc = (struct umass_softc *) addr;
2680 struct cam_path *path;
2683 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2684 cam_sim_path(sc->umass_sim),
2685 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2686 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2688 ccb = malloc(sizeof(union ccb), M_USBDEV, M_NOWAIT | M_ZERO);
2691 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2692 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD)
2694 free(ccb, M_USBDEV);
2698 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2699 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2700 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2701 ccb->crcn.flags = CAM_FLAG_NONE;
2704 /* The scan is in progress now. */
2708 umass_cam_attach(struct umass_softc *sc)
2713 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2714 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2715 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2716 cam_sim_path(sc->umass_sim));
2719 /* Notify CAM of the new device after a short delay. Any
2720 * failure is benign, as the user can still do it by hand
2721 * (camcontrol rescan <busno>). Only do this if we are not
2722 * booting, because CAM does a scan after booting has
2723 * completed, when interrupts have been enabled.
2726 callout_reset(&sc->cam_scsi_rescan_ch, MS_TO_TICKS(200),
2727 umass_cam_rescan, sc);
2730 return(0); /* always succesfull */
2734 * detach from the CAM layer
2738 umass_cam_detach_sim(struct umass_softc *sc)
2740 if (sc->umass_sim) {
2741 if (xpt_bus_deregister(cam_sim_path(sc->umass_sim)))
2742 cam_sim_free(sc->umass_sim, /*free_devq*/TRUE);
2746 sc->umass_sim = NULL;
2753 * CAM requests for action come through here
2757 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2759 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2761 /* The softc is still there, but marked as going away. umass_cam_detach
2762 * has not yet notified CAM of the lost device however.
2764 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2765 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2766 "Invalid target (gone)\n",
2767 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2768 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2769 ccb->ccb_h.func_code));
2770 ccb->ccb_h.status = CAM_TID_INVALID;
2775 /* Verify, depending on the operation to perform, that we either got a
2776 * valid sc, because an existing target was referenced, or otherwise
2777 * the SIM is addressed.
2779 * This avoids bombing out at a printf and does give the CAM layer some
2780 * sensible feedback on errors.
2782 switch (ccb->ccb_h.func_code) {
2785 case XPT_GET_TRAN_SETTINGS:
2786 case XPT_SET_TRAN_SETTINGS:
2787 case XPT_CALC_GEOMETRY:
2788 /* the opcodes requiring a target. These should never occur. */
2790 printf("%s:%d:%d:%d:func_code 0x%04x: "
2791 "Invalid target (target needed)\n",
2792 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2793 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2794 ccb->ccb_h.func_code);
2796 ccb->ccb_h.status = CAM_TID_INVALID;
2803 /* The opcodes sometimes aimed at a target (sc is valid),
2804 * sometimes aimed at the SIM (sc is invalid and target is
2805 * CAM_TARGET_WILDCARD)
2807 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2808 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2809 "Invalid target (no wildcard)\n",
2810 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2811 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2812 ccb->ccb_h.func_code));
2814 ccb->ccb_h.status = CAM_TID_INVALID;
2820 /* XXX Hm, we should check the input parameters */
2824 /* Perform the requested action */
2825 switch (ccb->ccb_h.func_code) {
2828 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2832 unsigned char *rcmd;
2835 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2836 "cmd: 0x%02x, flags: 0x%02x, "
2837 "%db cmd/%db data/%db sense\n",
2838 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2839 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2840 csio->cdb_io.cdb_bytes[0],
2841 ccb->ccb_h.flags & CAM_DIR_MASK,
2842 csio->cdb_len, csio->dxfer_len,
2845 /* clear the end of the buffer to make sure we don't send out
2848 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2850 umass_dump_buffer(sc, csio->data_ptr,
2851 csio->dxfer_len, 48));
2853 if (sc->transfer_state != TSTATE_IDLE) {
2854 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2855 "I/O in progress, deferring (state %d, %s)\n",
2856 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2857 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2858 sc->transfer_state,states[sc->transfer_state]));
2859 ccb->ccb_h.status = CAM_SCSI_BUSY;
2864 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2875 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2878 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2879 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2881 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2883 cmdlen = csio->cdb_len;
2884 rcmd = (unsigned char *) &sc->cam_scsi_command;
2885 rcmdlen = sizeof(sc->cam_scsi_command);
2887 /* sc->transform will convert the command to the command
2888 * (format) needed by the specific command set and return
2889 * the converted command in a buffer pointed to be rcmd.
2890 * We pass in a buffer, but if the command does not
2891 * have to be transformed it returns a ptr to the original
2892 * buffer (see umass_scsi_transform).
2895 switch (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2898 * Handle EVPD inquiry for broken devices first
2899 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2901 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2902 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2903 struct scsi_sense_data *sense;
2905 sense = &ccb->csio.sense_data;
2906 bzero(sense, sizeof(*sense));
2907 sense->error_code = SSD_CURRENT_ERROR;
2908 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2909 sense->add_sense_code = 0x24;
2910 sense->extra_len = 10;
2911 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2912 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2917 /* Return fake inquiry data for broken devices */
2918 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
2919 struct ccb_scsiio *csio = &ccb->csio;
2921 memcpy(csio->data_ptr, &fake_inq_data,
2922 sizeof(fake_inq_data));
2923 csio->scsi_status = SCSI_STATUS_OK;
2924 ccb->ccb_h.status = CAM_REQ_CMP;
2928 if ((sc->quirks & NO_SYNCHRONIZE_CACHE) &&
2929 rcmd[0] == SYNCHRONIZE_CACHE) {
2930 struct ccb_scsiio *csio = &ccb->csio;
2932 csio->scsi_status = SCSI_STATUS_OK;
2933 ccb->ccb_h.status = CAM_REQ_CMP;
2937 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
2938 rcmd[0] == INQUIRY) {
2939 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
2941 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
2943 csio->dxfer_len, dir, ccb->ccb_h.timeout,
2944 umass_cam_cb, (void *) ccb);
2947 ccb->ccb_h.status = CAM_REQ_INVALID;
2951 ccb->ccb_h.status = CAM_REQ_CMP;
2960 struct ccb_pathinq *cpi = &ccb->cpi;
2962 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
2963 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
2964 cam_sim_path(sc->umass_sim),
2965 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2967 /* host specific information */
2968 cpi->version_num = 1;
2969 cpi->hba_inquiry = 0;
2970 cpi->target_sprt = 0;
2971 cpi->hba_misc = PIM_NO_6_BYTE;
2972 cpi->hba_eng_cnt = 0;
2973 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2974 cpi->initiator_id = UMASS_SCSIID_HOST;
2975 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2976 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2977 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2978 cpi->unit_number = cam_sim_unit(sim);
2979 cpi->bus_id = device_get_unit(sc->sc_dev);
2980 cpi->protocol = PROTO_SCSI;
2981 cpi->protocol_version = SCSI_REV_2;
2982 cpi->transport = XPORT_USB;
2983 cpi->transport_version = 0;
2986 cpi->base_transfer_speed = 0;
2989 if (sc->quirks & FLOPPY_SPEED) {
2990 cpi->base_transfer_speed =
2991 UMASS_FLOPPY_TRANSFER_SPEED;
2992 } else if (usbd_get_speed(sc->sc_udev) ==
2994 cpi->base_transfer_speed =
2995 UMASS_HIGH_TRANSFER_SPEED;
2997 cpi->base_transfer_speed =
2998 UMASS_FULL_TRANSFER_SPEED;
3000 cpi->max_lun = sc->maxlun;
3003 cpi->ccb_h.status = CAM_REQ_CMP;
3009 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
3010 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3011 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3013 ccb->ccb_h.status = CAM_REQ_INPROG;
3014 umass_reset(sc, umass_cam_cb, (void *) ccb);
3017 case XPT_GET_TRAN_SETTINGS:
3019 struct ccb_trans_settings *cts = &ccb->cts;
3020 cts->protocol = PROTO_SCSI;
3021 cts->protocol_version = SCSI_REV_2;
3022 cts->transport = XPORT_USB;
3023 cts->transport_version = 0;
3024 cts->xport_specific.valid = 0;
3027 ccb->ccb_h.status = CAM_REQ_CMP;
3031 case XPT_SET_TRAN_SETTINGS:
3033 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3034 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3035 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3037 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3041 case XPT_CALC_GEOMETRY:
3043 cam_calc_geometry(&ccb->ccg, /*extended*/1);
3049 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
3050 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3051 cam_sim_path(sc->umass_sim),
3052 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3054 ccb->ccb_h.status = CAM_REQ_CMP;
3059 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
3060 "Not implemented\n",
3061 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3062 cam_sim_path(sc->umass_sim),
3063 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
3064 ccb->ccb_h.func_code));
3066 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3073 umass_cam_poll(struct cam_sim *sim)
3075 struct umass_softc *sc = (struct umass_softc *) sim->softc;
3077 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
3078 device_get_nameunit(sc->sc_dev)));
3080 usbd_set_polling(sc->sc_udev, 1);
3081 usbd_dopoll(sc->iface);
3082 usbd_set_polling(sc->sc_udev, 0);
3087 * finalise a completed CAM command
3091 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
3093 union ccb *ccb = (union ccb *) priv;
3094 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3096 /* If the device is gone, just fail the request. */
3097 if (sc->flags & UMASS_FLAGS_GONE) {
3098 ccb->ccb_h.status = CAM_TID_INVALID;
3103 csio->resid = residue;
3107 ccb->ccb_h.status = CAM_REQ_CMP;
3108 if ((sc->quirks & READ_CAPACITY_OFFBY1) &&
3109 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3110 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3111 struct scsi_read_capacity_data *rcap;
3114 rcap = (struct scsi_read_capacity_data *)csio->data_ptr;
3115 maxsector = scsi_4btoul(rcap->addr) - 1;
3116 scsi_ulto4b(maxsector, rcap->addr);
3121 case STATUS_CMD_UNKNOWN:
3122 case STATUS_CMD_FAILED:
3123 switch (ccb->ccb_h.func_code) {
3126 unsigned char *rcmd;
3129 /* fetch sense data */
3130 /* the rest of the command was filled in at attach */
3131 sc->cam_scsi_sense.length = csio->sense_len;
3133 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
3134 device_get_nameunit(sc->sc_dev), csio->sense_len));
3136 rcmd = (unsigned char *) &sc->cam_scsi_command;
3137 rcmdlen = sizeof(sc->cam_scsi_command);
3139 if (sc->transform(sc,
3140 (unsigned char *) &sc->cam_scsi_sense,
3141 sizeof(sc->cam_scsi_sense),
3142 &rcmd, &rcmdlen) == 1) {
3143 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
3144 csio->sense_len = SHORT_INQUIRY_LENGTH;
3146 sc->transfer(sc, ccb->ccb_h.target_lun,
3149 csio->sense_len, DIR_IN, ccb->ccb_h.timeout,
3150 umass_cam_sense_cb, (void *) ccb);
3152 panic("transform(REQUEST_SENSE) failed");
3156 case XPT_RESET_DEV: /* Reset failed */
3157 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3161 panic("umass_cam_cb called for func_code %d",
3162 ccb->ccb_h.func_code);
3166 case STATUS_WIRE_FAILED:
3167 /* the wire protocol failed and will have recovered
3168 * (hopefully). We return an error to CAM and let CAM retry
3169 * the command if necessary.
3171 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3175 panic("%s: Unknown status %d in umass_cam_cb",
3176 device_get_nameunit(sc->sc_dev), status);
3180 /* Finalise a completed autosense operation
3183 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
3185 union ccb *ccb = (union ccb *) priv;
3186 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3187 unsigned char *rcmd;
3190 if (sc->flags & UMASS_FLAGS_GONE) {
3191 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3198 case STATUS_CMD_UNKNOWN:
3199 case STATUS_CMD_FAILED:
3200 /* Getting sense data always succeeds (apart from wire
3203 if ((sc->quirks & RS_NO_CLEAR_UA)
3204 && csio->cdb_io.cdb_bytes[0] == INQUIRY
3205 && (csio->sense_data.flags & SSD_KEY)
3206 == SSD_KEY_UNIT_ATTENTION) {
3207 /* Ignore unit attention errors in the case where
3208 * the Unit Attention state is not cleared on
3209 * REQUEST SENSE. They will appear again at the next
3212 ccb->ccb_h.status = CAM_REQ_CMP;
3213 } else if ((csio->sense_data.flags & SSD_KEY)
3214 == SSD_KEY_NO_SENSE) {
3215 /* No problem after all (in the case of CBI without
3218 ccb->ccb_h.status = CAM_REQ_CMP;
3219 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
3220 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
3221 ((csio->sense_data.flags & SSD_KEY)
3222 == SSD_KEY_UNIT_ATTENTION)) {
3224 * Some devices do not clear the unit attention error
3225 * on request sense. We insert a test unit ready
3226 * command to make sure we clear the unit attention
3227 * condition, then allow the retry to proceed as
3231 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3232 | CAM_AUTOSNS_VALID;
3233 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3239 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
3240 "TEST_UNIT_READY\n",
3241 device_get_nameunit(sc->sc_dev)));
3243 /* the rest of the command was filled in at attach */
3245 rcmd = (unsigned char *) &sc->cam_scsi_command2;
3246 rcmdlen = sizeof(sc->cam_scsi_command2);
3248 if (sc->transform(sc,
3250 &sc->cam_scsi_test_unit_ready,
3251 sizeof(sc->cam_scsi_test_unit_ready),
3252 &rcmd, &rcmdlen) == 1) {
3253 sc->transfer(sc, ccb->ccb_h.target_lun,
3255 NULL, 0, DIR_NONE, ccb->ccb_h.timeout,
3256 umass_cam_quirk_cb, (void *) ccb);
3258 panic("transform(TEST_UNIT_READY) failed");
3262 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3263 | CAM_AUTOSNS_VALID;
3264 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3270 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3271 device_get_nameunit(sc->sc_dev), status));
3272 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3278 * This completion code just handles the fact that we sent a test-unit-ready
3279 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3280 * though this command succeeded, we have to tell CAM to retry.
3283 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
3285 union ccb *ccb = (union ccb *) priv;
3287 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
3288 device_get_nameunit(sc->sc_dev), status));
3290 if (sc->flags & UMASS_FLAGS_GONE) {
3291 ccb->ccb_h.status = CAM_TID_INVALID;
3296 ccb->ccb_h.status = CAM_REQ_CMP;
3298 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3299 | CAM_AUTOSNS_VALID;
3300 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3305 * SCSI specific functions
3309 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3310 unsigned char **rcmd, int *rcmdlen)
3313 case TEST_UNIT_READY:
3314 if (sc->quirks & NO_TEST_UNIT_READY) {
3315 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3316 ("rcmdlen = %d < %ld, buffer too small",
3318 (long)sizeof(struct scsi_start_stop_unit)));
3319 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3320 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3321 memset(*rcmd, 0, *rcmdlen);
3322 (*rcmd)[0] = START_STOP_UNIT;
3323 (*rcmd)[4] = SSS_START;
3328 /* some drives wedge when asked for full inquiry information. */
3329 if (sc->quirks & FORCE_SHORT_INQUIRY) {
3330 memcpy(*rcmd, cmd, cmdlen);
3332 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3337 *rcmd = cmd; /* We don't need to copy it */
3343 /* RBC specific functions */
3345 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3346 unsigned char **rcmd, int *rcmdlen)
3349 /* these commands are defined in RBC: */
3352 case START_STOP_UNIT:
3353 case SYNCHRONIZE_CACHE:
3355 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
3357 case MODE_SELECT_10:
3359 case TEST_UNIT_READY:
3361 /* The following commands are not listed in my copy of the RBC specs.
3362 * CAM however seems to want those, and at least the Sony DSC device
3363 * appears to support those as well */
3366 if ((sc->quirks & RBC_PAD_TO_12) && cmdlen < 12) {
3368 bcopy(cmd, *rcmd, cmdlen);
3369 bzero(*rcmd + cmdlen, 12 - cmdlen);
3371 *rcmd = cmd; /* We don't need to copy it */
3375 /* All other commands are not legal in RBC */
3377 printf("%s: Unsupported RBC command 0x%02x",
3378 device_get_nameunit(sc->sc_dev), cmd[0]);
3380 return 0; /* failure */
3385 * UFI specific functions
3388 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3389 unsigned char **rcmd, int *rcmdlen)
3391 /* A UFI command is always 12 bytes in length */
3392 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
3393 ("rcmdlen = %d < %d, buffer too small",
3394 *rcmdlen, UFI_COMMAND_LENGTH));
3396 *rcmdlen = UFI_COMMAND_LENGTH;
3397 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
3400 /* Commands of which the format has been verified. They should work.
3401 * Copy the command into the (zeroed out) destination buffer.
3403 case TEST_UNIT_READY:
3404 if (sc->quirks & NO_TEST_UNIT_READY) {
3405 /* Some devices do not support this command.
3406 * Start Stop Unit should give the same results
3408 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3409 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3410 (*rcmd)[0] = START_STOP_UNIT;
3411 (*rcmd)[4] = SSS_START;
3413 memcpy(*rcmd, cmd, cmdlen);
3421 case START_STOP_UNIT:
3422 case SEND_DIAGNOSTIC:
3427 case POSITION_TO_ELEMENT: /* SEEK_10 */
3428 case WRITE_AND_VERIFY:
3430 case MODE_SELECT_10:
3434 case READ_FORMAT_CAPACITIES:
3435 memcpy(*rcmd, cmd, cmdlen);
3439 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3440 * required for UFI devices, so it is appropriate to fake
3443 case SYNCHRONIZE_CACHE:
3447 printf("%s: Unsupported UFI command 0x%02x\n",
3448 device_get_nameunit(sc->sc_dev), cmd[0]);
3449 return 0; /* failure */
3454 * 8070i (ATAPI) specific functions
3457 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3458 unsigned char **rcmd, int *rcmdlen)
3460 /* An ATAPI command is always 12 bytes in length. */
3461 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
3462 ("rcmdlen = %d < %d, buffer too small",
3463 *rcmdlen, ATAPI_COMMAND_LENGTH));
3465 *rcmdlen = ATAPI_COMMAND_LENGTH;
3466 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
3469 /* Commands of which the format has been verified. They should work.
3470 * Copy the command into the (zeroed out) destination buffer.
3473 memcpy(*rcmd, cmd, cmdlen);
3474 /* some drives wedge when asked for full inquiry information. */
3475 if (sc->quirks & FORCE_SHORT_INQUIRY)
3476 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3479 case TEST_UNIT_READY:
3480 if (sc->quirks & NO_TEST_UNIT_READY) {
3481 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3482 ("rcmdlen = %d < %ld, buffer too small",
3484 (long)sizeof(struct scsi_start_stop_unit)));
3485 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3486 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3487 memset(*rcmd, 0, *rcmdlen);
3488 (*rcmd)[0] = START_STOP_UNIT;
3489 (*rcmd)[4] = SSS_START;
3495 case START_STOP_UNIT:
3496 case SEND_DIAGNOSTIC:
3501 case POSITION_TO_ELEMENT: /* SEEK_10 */
3502 case SYNCHRONIZE_CACHE:
3503 case MODE_SELECT_10:
3506 case 0x42: /* READ_SUBCHANNEL */
3507 case 0x43: /* READ_TOC */
3508 case 0x44: /* READ_HEADER */
3509 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
3510 case 0x48: /* PLAY_TRACK */
3511 case 0x49: /* PLAY_TRACK_REL */
3512 case 0x4b: /* PAUSE */
3513 case 0x51: /* READ_DISK_INFO */
3514 case 0x52: /* READ_TRACK_INFO */
3515 case 0x54: /* SEND_OPC */
3516 case 0x59: /* READ_MASTER_CUE */
3517 case 0x5b: /* CLOSE_TR_SESSION */
3518 case 0x5c: /* READ_BUFFER_CAP */
3519 case 0x5d: /* SEND_CUE_SHEET */
3520 case 0xa1: /* BLANK */
3521 case 0xa5: /* PLAY_12 */
3522 case 0xa6: /* EXCHANGE_MEDIUM */
3523 case 0xad: /* READ_DVD_STRUCTURE */
3524 case 0xbb: /* SET_CD_SPEED */
3525 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
3526 memcpy(*rcmd, cmd, cmdlen);
3532 printf("%s: Unsupported ATAPI command 0x%02x"
3533 " - trying anyway\n",
3534 device_get_nameunit(sc->sc_dev), cmd[0]);
3535 memcpy(*rcmd, cmd, cmdlen);
3541 /* (even the comment is missing) */
3543 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, usbd_driver_load, 0);
3549 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3551 int clen = cbw->bCDBLength;
3552 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3553 u_int8_t *c = cbw->CBWCDB;
3554 int tag = UGETDW(cbw->dCBWTag);
3555 int flags = cbw->bCBWFlags;
3557 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3558 "(0x%02x%02x%02x%02x%02x%02x%s), "
3559 "data = %db, dir = %s\n",
3560 device_get_nameunit(sc->sc_dev), tag, clen,
3561 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3562 dlen, (flags == CBWFLAGS_IN? "in":
3563 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3567 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3569 int sig = UGETDW(csw->dCSWSignature);
3570 int tag = UGETW(csw->dCSWTag);
3571 int res = UGETDW(csw->dCSWDataResidue);
3572 int status = csw->bCSWStatus;
3574 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3575 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3576 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3578 status, (status == CSWSTATUS_GOOD? "good":
3579 (status == CSWSTATUS_FAILED? "failed":
3580 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3584 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3587 int dir = sc->transfer_dir;
3589 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3590 "(0x%02x%02x%02x%02x%02x%02x%s), "
3591 "data = %db, dir = %s\n",
3592 device_get_nameunit(sc->sc_dev), cmdlen,
3593 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3594 sc->transfer_datalen,
3595 (dir == DIR_IN? "in":
3596 (dir == DIR_OUT? "out":
3597 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3601 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3612 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3613 for (i = 0; i < buflen && i < printlen; i++) {
3615 if (j == 0 && i != 0) {
3616 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3617 device_get_nameunit(sc->sc_dev), s1, s2));
3620 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3622 if (buflen > printlen)
3623 sprintf(s3, " ...");
3624 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3625 device_get_nameunit(sc->sc_dev), s1, s2, s3));