]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/storage/umass.c
MFC r199816
[FreeBSD/stable/8.git] / sys / dev / usb / storage / umass.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 /*-
5  * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
6  *                    Nick Hibma <n_hibma@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      $FreeBSD$
31  *      $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
32  */
33
34 /* Also already merged from NetBSD:
35  *      $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
36  *      $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
37  *      $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
38  *      $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
39  */
40
41 /*
42  * Universal Serial Bus Mass Storage Class specs:
43  * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
44  * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
45  * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
46  * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
47  */
48
49 /*
50  * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
51  * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
52  */
53
54 /*
55  * The driver handles 3 Wire Protocols
56  * - Command/Bulk/Interrupt (CBI)
57  * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
58  * - Mass Storage Bulk-Only (BBB)
59  *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
60  *
61  * Over these wire protocols it handles the following command protocols
62  * - SCSI
63  * - UFI (floppy command set)
64  * - 8070i (ATAPI)
65  *
66  * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
67  * sc->sc_transform method is used to convert the commands into the appropriate
68  * format (if at all necessary). For example, UFI requires all commands to be
69  * 12 bytes in length amongst other things.
70  *
71  * The source code below is marked and can be split into a number of pieces
72  * (in this order):
73  *
74  * - probe/attach/detach
75  * - generic transfer routines
76  * - BBB
77  * - CBI
78  * - CBI_I (in addition to functions from CBI)
79  * - CAM (Common Access Method)
80  * - SCSI
81  * - UFI
82  * - 8070i (ATAPI)
83  *
84  * The protocols are implemented using a state machine, for the transfers as
85  * well as for the resets. The state machine is contained in umass_t_*_callback.
86  * The state machine is started through either umass_command_start() or
87  * umass_reset().
88  *
89  * The reason for doing this is a) CAM performs a lot better this way and b) it
90  * avoids using tsleep from interrupt context (for example after a failed
91  * transfer).
92  */
93
94 /*
95  * The SCSI related part of this driver has been derived from the
96  * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
97  *
98  * The CAM layer uses so called actions which are messages sent to the host
99  * adapter for completion. The actions come in through umass_cam_action. The
100  * appropriate block of routines is called depending on the transport protocol
101  * in use. When the transfer has finished, these routines call
102  * umass_cam_cb again to complete the CAM command.
103  */
104
105 #include <sys/stdint.h>
106 #include <sys/stddef.h>
107 #include <sys/param.h>
108 #include <sys/queue.h>
109 #include <sys/types.h>
110 #include <sys/systm.h>
111 #include <sys/kernel.h>
112 #include <sys/bus.h>
113 #include <sys/linker_set.h>
114 #include <sys/module.h>
115 #include <sys/lock.h>
116 #include <sys/mutex.h>
117 #include <sys/condvar.h>
118 #include <sys/sysctl.h>
119 #include <sys/sx.h>
120 #include <sys/unistd.h>
121 #include <sys/callout.h>
122 #include <sys/malloc.h>
123 #include <sys/priv.h>
124
125 #include <dev/usb/usb.h>
126 #include <dev/usb/usbdi.h>
127 #include <dev/usb/usb_device.h>
128 #include "usbdevs.h"
129
130 #include <cam/cam.h>
131 #include <cam/cam_ccb.h>
132 #include <cam/cam_sim.h>
133 #include <cam/cam_xpt_sim.h>
134 #include <cam/scsi/scsi_all.h>
135 #include <cam/scsi/scsi_da.h>
136
137 #include <cam/cam_periph.h>
138
139 #define UMASS_EXT_BUFFER
140 #ifdef UMASS_EXT_BUFFER
141 /* this enables loading of virtual buffers into DMA */
142 #define UMASS_USB_FLAGS .ext_buffer=1,
143 #else
144 #define UMASS_USB_FLAGS
145 #endif
146
147 #if USB_DEBUG
148 #define DIF(m, x)                               \
149   do {                                          \
150     if (umass_debug & (m)) { x ; }              \
151   } while (0)
152
153 #define DPRINTF(sc, m, fmt, ...)                        \
154   do {                                                  \
155     if (umass_debug & (m)) {                            \
156         printf("%s:%s: " fmt,                           \
157                (sc) ? (const char *)(sc)->sc_name :     \
158                (const char *)"umassX",                  \
159                 __FUNCTION__ ,## __VA_ARGS__);          \
160     }                                                   \
161   } while (0)
162
163 #define UDMASS_GEN      0x00010000      /* general */
164 #define UDMASS_SCSI     0x00020000      /* scsi */
165 #define UDMASS_UFI      0x00040000      /* ufi command set */
166 #define UDMASS_ATAPI    0x00080000      /* 8070i command set */
167 #define UDMASS_CMD      (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
168 #define UDMASS_USB      0x00100000      /* USB general */
169 #define UDMASS_BBB      0x00200000      /* Bulk-Only transfers */
170 #define UDMASS_CBI      0x00400000      /* CBI transfers */
171 #define UDMASS_WIRE     (UDMASS_BBB|UDMASS_CBI)
172 #define UDMASS_ALL      0xffff0000      /* all of the above */
173 static int umass_debug = 0;
174
175 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
176 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
177     &umass_debug, 0, "umass debug level");
178
179 TUNABLE_INT("hw.usb.umass.debug", &umass_debug);
180 #else
181 #define DIF(...) do { } while (0)
182 #define DPRINTF(...) do { } while (0)
183 #endif
184
185 #define UMASS_GONE ((struct umass_softc *)1)
186
187 #define UMASS_BULK_SIZE (1 << 17)
188 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12  /* bytes */
189 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN)        /* bytes */
190
191 /* USB transfer definitions */
192
193 #define UMASS_T_BBB_RESET1      0       /* Bulk-Only */
194 #define UMASS_T_BBB_RESET2      1
195 #define UMASS_T_BBB_RESET3      2
196 #define UMASS_T_BBB_COMMAND     3
197 #define UMASS_T_BBB_DATA_READ   4
198 #define UMASS_T_BBB_DATA_RD_CS  5
199 #define UMASS_T_BBB_DATA_WRITE  6
200 #define UMASS_T_BBB_DATA_WR_CS  7
201 #define UMASS_T_BBB_STATUS      8
202 #define UMASS_T_BBB_MAX         9
203
204 #define UMASS_T_CBI_RESET1      0       /* CBI */
205 #define UMASS_T_CBI_RESET2      1
206 #define UMASS_T_CBI_RESET3      2
207 #define UMASS_T_CBI_COMMAND     3
208 #define UMASS_T_CBI_DATA_READ   4
209 #define UMASS_T_CBI_DATA_RD_CS  5
210 #define UMASS_T_CBI_DATA_WRITE  6
211 #define UMASS_T_CBI_DATA_WR_CS  7
212 #define UMASS_T_CBI_STATUS      8
213 #define UMASS_T_CBI_RESET4      9
214 #define UMASS_T_CBI_MAX        10
215
216 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
217
218 /* Generic definitions */
219
220 /* Direction for transfer */
221 #define DIR_NONE        0
222 #define DIR_IN          1
223 #define DIR_OUT         2
224
225 /* device name */
226 #define DEVNAME         "umass"
227 #define DEVNAME_SIM     "umass-sim"
228
229 /* Approximate maximum transfer speeds (assumes 33% overhead). */
230 #define UMASS_FULL_TRANSFER_SPEED       1000
231 #define UMASS_HIGH_TRANSFER_SPEED       40000
232 #define UMASS_FLOPPY_TRANSFER_SPEED     20
233
234 #define UMASS_TIMEOUT                   5000    /* ms */
235
236 /* CAM specific definitions */
237
238 #define UMASS_SCSIID_MAX        1       /* maximum number of drives expected */
239 #define UMASS_SCSIID_HOST       UMASS_SCSIID_MAX
240
241 /* Bulk-Only features */
242
243 #define UR_BBB_RESET            0xff    /* Bulk-Only reset */
244 #define UR_BBB_GET_MAX_LUN      0xfe    /* Get maximum lun */
245
246 /* Command Block Wrapper */
247 typedef struct {
248         uDWord  dCBWSignature;
249 #define CBWSIGNATURE    0x43425355
250         uDWord  dCBWTag;
251         uDWord  dCBWDataTransferLength;
252         uByte   bCBWFlags;
253 #define CBWFLAGS_OUT    0x00
254 #define CBWFLAGS_IN     0x80
255         uByte   bCBWLUN;
256         uByte   bCDBLength;
257 #define CBWCDBLENGTH    16
258         uByte   CBWCDB[CBWCDBLENGTH];
259 } __packed umass_bbb_cbw_t;
260
261 #define UMASS_BBB_CBW_SIZE      31
262
263 /* Command Status Wrapper */
264 typedef struct {
265         uDWord  dCSWSignature;
266 #define CSWSIGNATURE    0x53425355
267 #define CSWSIGNATURE_IMAGINATION_DBX1   0x43425355
268 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
269         uDWord  dCSWTag;
270         uDWord  dCSWDataResidue;
271         uByte   bCSWStatus;
272 #define CSWSTATUS_GOOD  0x0
273 #define CSWSTATUS_FAILED        0x1
274 #define CSWSTATUS_PHASE 0x2
275 } __packed umass_bbb_csw_t;
276
277 #define UMASS_BBB_CSW_SIZE      13
278
279 /* CBI features */
280
281 #define UR_CBI_ADSC     0x00
282
283 typedef union {
284         struct {
285                 uint8_t type;
286 #define IDB_TYPE_CCI            0x00
287                 uint8_t value;
288 #define IDB_VALUE_PASS          0x00
289 #define IDB_VALUE_FAIL          0x01
290 #define IDB_VALUE_PHASE         0x02
291 #define IDB_VALUE_PERSISTENT    0x03
292 #define IDB_VALUE_STATUS_MASK   0x03
293         } __packed common;
294
295         struct {
296                 uint8_t asc;
297                 uint8_t ascq;
298         } __packed ufi;
299 } __packed umass_cbi_sbl_t;
300
301 struct umass_softc;                     /* see below */
302
303 typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
304         uint32_t residue, uint8_t status);
305
306 #define STATUS_CMD_OK           0       /* everything ok */
307 #define STATUS_CMD_UNKNOWN      1       /* will have to fetch sense */
308 #define STATUS_CMD_FAILED       2       /* transfer was ok, command failed */
309 #define STATUS_WIRE_FAILED      3       /* couldn't even get command across */
310
311 typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
312         uint8_t cmd_len);
313
314 struct umass_devdescr {
315         uint32_t vid;
316 #define VID_WILDCARD    0xffffffff
317 #define VID_EOT         0xfffffffe
318         uint32_t pid;
319 #define PID_WILDCARD    0xffffffff
320 #define PID_EOT         0xfffffffe
321         uint32_t rid;
322 #define RID_WILDCARD    0xffffffff
323 #define RID_EOT         0xfffffffe
324
325         /* wire and command protocol */
326         uint16_t proto;
327 #define UMASS_PROTO_DEFAULT     0x0000  /* use protocol indicated by USB descriptors */
328 #define UMASS_PROTO_BBB         0x0001  /* USB wire protocol */
329 #define UMASS_PROTO_CBI         0x0002
330 #define UMASS_PROTO_CBI_I       0x0004
331 #define UMASS_PROTO_WIRE                0x00ff  /* USB wire protocol mask */
332 #define UMASS_PROTO_SCSI                0x0100  /* command protocol */
333 #define UMASS_PROTO_ATAPI       0x0200
334 #define UMASS_PROTO_UFI         0x0400
335 #define UMASS_PROTO_RBC         0x0800
336 #define UMASS_PROTO_COMMAND     0xff00  /* command protocol mask */
337
338         /* Device specific quirks */
339         uint16_t quirks;
340 #define NO_QUIRKS               0x0000
341         /*
342          * The drive does not support Test Unit Ready. Convert to Start Unit
343          */
344 #define NO_TEST_UNIT_READY      0x0001
345         /*
346          * The drive does not reset the Unit Attention state after REQUEST
347          * SENSE has been sent. The INQUIRY command does not reset the UA
348          * either, and so CAM runs in circles trying to retrieve the initial
349          * INQUIRY data.
350          */
351 #define RS_NO_CLEAR_UA          0x0002
352         /* The drive does not support START STOP.  */
353 #define NO_START_STOP           0x0004
354         /* Don't ask for full inquiry data (255b).  */
355 #define FORCE_SHORT_INQUIRY     0x0008
356         /* Needs to be initialised the Shuttle way */
357 #define SHUTTLE_INIT            0x0010
358         /* Drive needs to be switched to alternate iface 1 */
359 #define ALT_IFACE_1             0x0020
360         /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
361 #define FLOPPY_SPEED            0x0040
362         /* The device can't count and gets the residue of transfers wrong */
363 #define IGNORE_RESIDUE          0x0080
364         /* No GetMaxLun call */
365 #define NO_GETMAXLUN            0x0100
366         /* The device uses a weird CSWSIGNATURE. */
367 #define WRONG_CSWSIG            0x0200
368         /* Device cannot handle INQUIRY so fake a generic response */
369 #define NO_INQUIRY              0x0400
370         /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
371 #define NO_INQUIRY_EVPD         0x0800
372         /* Pad all RBC requests to 12 bytes. */
373 #define RBC_PAD_TO_12           0x1000
374         /*
375          * Device reports number of sectors from READ_CAPACITY, not max
376          * sector number.
377          */
378 #define READ_CAPACITY_OFFBY1    0x2000
379         /*
380          * Device cannot handle a SCSI synchronize cache command.  Normally
381          * this quirk would be handled in the cam layer, but for IDE bridges
382          * we need to associate the quirk with the bridge and not the
383          * underlying disk device.  This is handled by faking a success
384          * result.
385          */
386 #define NO_SYNCHRONIZE_CACHE    0x4000
387 };
388
389 static const struct umass_devdescr umass_devdescr[] = {
390         {USB_VENDOR_ASAHIOPTICAL, PID_WILDCARD, RID_WILDCARD,
391                 UMASS_PROTO_DEFAULT,
392                 RS_NO_CLEAR_UA
393         },
394         {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_ATTACHE, RID_WILDCARD,
395                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
396                 IGNORE_RESIDUE
397         },
398         {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_A256MB, RID_WILDCARD,
399                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
400                 IGNORE_RESIDUE
401         },
402         {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_DISKPRO512, RID_WILDCARD,
403                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
404                 IGNORE_RESIDUE
405         },
406         {USB_VENDOR_ADDONICS2, USB_PRODUCT_ADDONICS2_CABLE_205, RID_WILDCARD,
407                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
408                 NO_QUIRKS
409         },
410         {USB_VENDOR_AIPTEK, USB_PRODUCT_AIPTEK_POCKETCAM3M, RID_WILDCARD,
411                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
412                 NO_QUIRKS
413         },
414         {USB_VENDOR_AIPTEK2, USB_PRODUCT_AIPTEK2_SUNPLUS_TECH, RID_WILDCARD,
415                 UMASS_PROTO_DEFAULT,
416                 NO_SYNCHRONIZE_CACHE
417         },
418         {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_SDCR_6335, RID_WILDCARD,
419                 UMASS_PROTO_DEFAULT,
420                 NO_TEST_UNIT_READY | NO_SYNCHRONIZE_CACHE
421         },
422         {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_AU6390, RID_WILDCARD,
423                 UMASS_PROTO_DEFAULT,
424                 NO_SYNCHRONIZE_CACHE
425         },
426         {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_UMCR_9361, RID_WILDCARD,
427                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
428                 NO_GETMAXLUN
429         },
430         {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_TRANSCEND, RID_WILDCARD,
431                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
432                 NO_GETMAXLUN
433         },
434         {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO230, RID_WILDCARD,
435                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
436                 NO_INQUIRY
437         },
438         {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO330, RID_WILDCARD,
439                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
440                 NO_INQUIRY
441         },
442         {USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2SCSI, RID_WILDCARD,
443                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
444                 NO_QUIRKS
445         },
446         {USB_VENDOR_CASIO, USB_PRODUCT_CASIO_QV_DIGICAM, RID_WILDCARD,
447                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
448                 NO_INQUIRY
449         },
450         {USB_VENDOR_CCYU, USB_PRODUCT_CCYU_ED1064, RID_WILDCARD,
451                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
452                 NO_QUIRKS
453         },
454         {USB_VENDOR_CENTURY, USB_PRODUCT_CENTURY_EX35QUAT, RID_WILDCARD,
455                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
456                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
457         },
458         {USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_XX6830XX, RID_WILDCARD,
459                 UMASS_PROTO_DEFAULT,
460                 NO_GETMAXLUN | NO_SYNCHRONIZE_CACHE
461         },
462         {USB_VENDOR_DESKNOTE, USB_PRODUCT_DESKNOTE_UCR_61S2B, RID_WILDCARD,
463                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
464                 NO_QUIRKS
465         },
466         {USB_VENDOR_DMI, USB_PRODUCT_DMI_CFSM_RW, RID_WILDCARD,
467                 UMASS_PROTO_SCSI,
468                 NO_GETMAXLUN
469         },
470         {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_875DC, RID_WILDCARD,
471                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
472                 NO_INQUIRY
473         },
474         {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_895, RID_WILDCARD,
475                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
476                 NO_GETMAXLUN
477         },
478         {USB_VENDOR_FEIYA, USB_PRODUCT_FEIYA_5IN1, RID_WILDCARD,
479                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
480                 NO_QUIRKS
481         },
482         {USB_VENDOR_FREECOM, USB_PRODUCT_FREECOM_DVD, RID_WILDCARD,
483                 UMASS_PROTO_SCSI,
484                 NO_QUIRKS
485         },
486         {USB_VENDOR_FUJIPHOTO, USB_PRODUCT_FUJIPHOTO_MASS0100, RID_WILDCARD,
487                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
488                 RS_NO_CLEAR_UA
489         },
490         {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE, RID_WILDCARD,
491                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
492                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
493                     | NO_SYNCHRONIZE_CACHE
494         },
495         {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE_2, RID_WILDCARD,
496                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
497                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
498         },
499         {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB, RID_WILDCARD,
500                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
501                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
502         },
503         {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB_2, RID_WILDCARD,
504                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
505                 WRONG_CSWSIG
506         },
507         {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FG, RID_WILDCARD,
508                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
509                 NO_QUIRKS
510         },
511         {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FGSM, RID_WILDCARD,
512                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
513                 NO_QUIRKS
514         },
515         {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_DZ_MV100A, RID_WILDCARD,
516                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
517                 NO_GETMAXLUN
518         },
519         {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_USB, RID_WILDCARD,
520                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
521                 NO_INQUIRY
522         },
523         {USB_VENDOR_HP, USB_PRODUCT_HP_CDW4E, RID_WILDCARD,
524                 UMASS_PROTO_ATAPI,
525                 NO_QUIRKS
526         },
527         {USB_VENDOR_HP, USB_PRODUCT_HP_CDW8200, RID_WILDCARD,
528                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
529                 NO_TEST_UNIT_READY | NO_START_STOP
530         },
531         {USB_VENDOR_IMAGINATION, USB_PRODUCT_IMAGINATION_DBX1, RID_WILDCARD,
532                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
533                 WRONG_CSWSIG
534         },
535         {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_USBCABLE, RID_WILDCARD,
536                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
537                 NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
538         },
539         {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_ATAPI, RID_WILDCARD,
540                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
541                 NO_QUIRKS
542         },
543         {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_STORAGE_V2, RID_WILDCARD,
544                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
545                 NO_QUIRKS
546         },
547         {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_IU_CD2, RID_WILDCARD,
548                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
549                 NO_QUIRKS
550         },
551         {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_DVR_UEH8, RID_WILDCARD,
552                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
553                 NO_QUIRKS
554         },
555         {USB_VENDOR_IOMEGA, USB_PRODUCT_IOMEGA_ZIP100, RID_WILDCARD,
556                 /*
557                  * XXX This is not correct as there are Zip drives that use
558                  * ATAPI.
559                  */
560                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
561                 NO_TEST_UNIT_READY
562         },
563         {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_L3, RID_WILDCARD,
564                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
565                 NO_INQUIRY
566         },
567         {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S3X, RID_WILDCARD,
568                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
569                 NO_INQUIRY
570         },
571         {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S4, RID_WILDCARD,
572                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
573                 NO_INQUIRY
574         },
575         {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S5, RID_WILDCARD,
576                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
577                 NO_INQUIRY
578         },
579         {USB_VENDOR_LACIE, USB_PRODUCT_LACIE_HD, RID_WILDCARD,
580                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
581                 NO_QUIRKS
582         },
583         {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_CF_READER, RID_WILDCARD,
584                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
585                 NO_INQUIRY
586         },
587         {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_JUMPSHOT, RID_WILDCARD,
588                 UMASS_PROTO_SCSI,
589                 NO_QUIRKS
590         },
591         {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443SU2, RID_WILDCARD,
592                 UMASS_PROTO_SCSI,
593                 NO_QUIRKS
594         },
595         {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443U2, RID_WILDCARD,
596                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
597                 NO_QUIRKS
598         },
599         {USB_VENDOR_MELCO, USB_PRODUCT_MELCO_DUBPXXG, RID_WILDCARD,
600                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
601                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
602         },
603         {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_DPCM, RID_WILDCARD,
604                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
605                 NO_TEST_UNIT_READY | NO_START_STOP
606         },
607         {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIDB25, RID_WILDCARD,
608                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
609                 NO_QUIRKS
610         },
611         {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIHD50, RID_WILDCARD,
612                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
613                 NO_QUIRKS
614         },
615         {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_E223, RID_WILDCARD,
616                 UMASS_PROTO_SCSI,
617                 NO_QUIRKS
618         },
619         {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_F300, RID_WILDCARD,
620                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
621                 NO_QUIRKS
622         },
623         {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_CDRRW, RID_WILDCARD,
624                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
625                 NO_QUIRKS
626         },
627         {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_FDD, RID_WILDCARD,
628                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
629                 NO_GETMAXLUN
630         },
631         {USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_E398, RID_WILDCARD,
632                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
633                 FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
634         },
635         {USB_VENDOR_MPMAN, PID_WILDCARD, RID_WILDCARD,
636                 UMASS_PROTO_DEFAULT,
637                 NO_SYNCHRONIZE_CACHE
638         },
639         {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY, RID_WILDCARD,
640                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
641                 IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
642         },
643         {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY2, RID_WILDCARD,
644                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
645                 NO_QUIRKS
646         },
647         {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN, RID_WILDCARD,
648                 UMASS_PROTO_DEFAULT,
649                 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
650         },
651         {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN_8813, RID_WILDCARD,
652                 UMASS_PROTO_DEFAULT,
653                 NO_SYNCHRONIZE_CACHE
654         },
655         {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_STARREADER, RID_WILDCARD,
656                 UMASS_PROTO_DEFAULT,
657                 NO_SYNCHRONIZE_CACHE
658         },
659         {USB_VENDOR_NEODIO, USB_PRODUCT_NEODIO_ND3260, RID_WILDCARD,
660                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
661                 FORCE_SHORT_INQUIRY
662         },
663         {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_CF_CARD, RID_WILDCARD,
664                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
665                 NO_INQUIRY
666         },
667         {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_ONLYDISK, RID_WILDCARD,
668                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
669                 IGNORE_RESIDUE
670         },
671         {USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_CLIK_40, RID_WILDCARD,
672                 UMASS_PROTO_ATAPI,
673                 NO_INQUIRY
674         },
675         {USB_VENDOR_NIKON, USB_PRODUCT_NIKON_D300, RID_WILDCARD,
676                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
677                 NO_QUIRKS
678         },
679         {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C1, RID_WILDCARD,
680                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
681                 WRONG_CSWSIG
682         },
683         {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C700, RID_WILDCARD,
684                 UMASS_PROTO_DEFAULT,
685                 NO_GETMAXLUN
686         },
687         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_SDS_HOTFIND_D, RID_WILDCARD,
688                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
689                 NO_GETMAXLUN | NO_SYNCHRONIZE_CACHE
690         },
691         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFMS_RW, RID_WILDCARD,
692                 UMASS_PROTO_SCSI,
693                 NO_QUIRKS
694         },
695         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_COMBO, RID_WILDCARD,
696                 UMASS_PROTO_SCSI,
697                 NO_QUIRKS
698         },
699         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER, RID_WILDCARD,
700                 UMASS_PROTO_SCSI,
701                 NO_QUIRKS
702         },
703         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER2, RID_WILDCARD,
704                 UMASS_PROTO_SCSI,
705                 NO_QUIRKS
706         },
707         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDCFE_B_CF_READER, RID_WILDCARD,
708                 UMASS_PROTO_SCSI,
709                 NO_QUIRKS
710         },
711         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDSM_B_READER, RID_WILDCARD,
712                 UMASS_PROTO_SCSI,
713                 NO_INQUIRY
714         },
715         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_READER, RID_WILDCARD,
716                 UMASS_PROTO_SCSI,
717                 NO_QUIRKS
718         },
719         {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_UCF100, RID_WILDCARD,
720                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
721                 NO_INQUIRY | NO_GETMAXLUN
722         },
723         {USB_VENDOR_ONSPEC2, USB_PRODUCT_ONSPEC2_IMAGEMATE_SDDR55, RID_WILDCARD,
724                 UMASS_PROTO_SCSI,
725                 NO_GETMAXLUN
726         },
727         {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXL840AN, RID_WILDCARD,
728                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
729                 NO_GETMAXLUN
730         },
731         {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB20AN, RID_WILDCARD,
732                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
733                 NO_QUIRKS
734         },
735         {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB35AN, RID_WILDCARD,
736                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737                 NO_QUIRKS
738         },
739         {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_LS120CAM, RID_WILDCARD,
740                 UMASS_PROTO_UFI,
741                 NO_QUIRKS
742         },
743         { USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_SPE3030CC, RID_WILDCARD,
744                 UMASS_PROTO_DEFAULT,
745                 NO_SYNCHRONIZE_CACHE
746         },
747         {USB_VENDOR_PLEXTOR, USB_PRODUCT_PLEXTOR_40_12_40U, RID_WILDCARD,
748                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
749                 NO_TEST_UNIT_READY
750         },
751         {USB_VENDOR_PNY, USB_PRODUCT_PNY_ATTACHE2, RID_WILDCARD,
752                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
753                 IGNORE_RESIDUE | NO_START_STOP
754         },
755         {USB_VENDOR_SAMSUNG_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
756                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
757                 NO_INQUIRY
758         },
759         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
760                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
761                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
762         },
763         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
764                 UMASS_PROTO_SCSI,
765                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
766         },
767         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
768                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
769                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
770         },
771         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
772                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
773                 IGNORE_RESIDUE
774         },
775         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
776                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
777                 IGNORE_RESIDUE
778         },
779         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
780                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
781                 IGNORE_RESIDUE
782         },
783         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
784                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
785                 READ_CAPACITY_OFFBY1
786         },
787         {USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
788                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
789                 NO_INQUIRY
790         },
791         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
792                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
793                 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
794         },
795         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
796                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
797                 NO_QUIRKS
798         },
799         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
800                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
801                 NO_QUIRKS
802         },
803         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
804                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
805                 NO_QUIRKS
806         },
807         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
808                 UMASS_PROTO_SCSI,
809                 NO_QUIRKS
810         },
811         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
812                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
813                 NO_QUIRKS
814         },
815         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
816                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
817                 NO_GETMAXLUN
818         },
819         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
820                 UMASS_PROTO_SCSI,
821                 NO_GETMAXLUN
822         },
823         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
824                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
825                 NO_GETMAXLUN
826         },
827         {USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
828                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
829                 SHUTTLE_INIT
830         },
831         {USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
832                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
833                 IGNORE_RESIDUE
834         },
835         {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
836                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
837                 NO_INQUIRY
838         },
839         {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
840                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
841                 NO_INQUIRY
842         },
843         {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, 0x0500,
844                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
845                 RBC_PAD_TO_12
846         },
847         {USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
848                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
849                 NO_INQUIRY
850         },
851         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
852                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
853                 RBC_PAD_TO_12
854         },
855         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
856                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
857                 RBC_PAD_TO_12
858         },
859         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
860                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
861                 NO_QUIRKS
862         },
863         {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
864                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
865                 NO_QUIRKS
866         },
867         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
868                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
869                 NO_QUIRKS
870         },
871         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
872                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
873                 NO_GETMAXLUN
874         },
875         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
876                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
877                 NO_GETMAXLUN
878         },
879         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
880                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
881                 NO_INQUIRY
882         },
883         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
884                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
885                 NO_GETMAXLUN
886         },
887         {USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
888                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
889                 NO_QUIRKS
890         },
891         {USB_VENDOR_SUPERTOP, USB_PRODUCT_SUPERTOP_IDE, RID_WILDCARD,
892                 UMASS_PROTO_DEFAULT,
893                 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
894         },
895         {USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
896                 UMASS_PROTO_SCSI,
897                 NO_QUIRKS
898         },
899         {USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
900                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
901                 NO_QUIRKS
902         },
903         {USB_VENDOR_TECLAST, USB_PRODUCT_TECLAST_TLC300, RID_WILDCARD,
904                 UMASS_PROTO_DEFAULT,
905                 NO_TEST_UNIT_READY | NO_SYNCHRONIZE_CACHE
906         },
907         {USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
908                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
909                 NO_INQUIRY
910         },
911         {USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
912                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
913                 IGNORE_RESIDUE
914         },
915         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
916                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
917                 NO_QUIRKS
918         },
919         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
920                 UMASS_PROTO_RBC,
921                 NO_QUIRKS
922         },
923         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
924                 UMASS_PROTO_SCSI,
925                 NO_QUIRKS
926         },
927         {USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
928                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
929                 NO_QUIRKS
930         },
931         {USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
932                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
933                 NO_SYNCHRONIZE_CACHE
934         },
935         {USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
936                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
937                 NO_INQUIRY
938         },
939         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
940                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
941                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
942         },
943         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, RID_WILDCARD,
944                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
945                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
946         },
947         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYBOOK, RID_WILDCARD,
948                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
949                 NO_INQUIRY_EVPD
950         },
951         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYPASSWORD, RID_WILDCARD,
952                 UMASS_PROTO_DEFAULT,
953                 FORCE_SHORT_INQUIRY
954         },
955         {USB_VENDOR_WINMAXGROUP, USB_PRODUCT_WINMAXGROUP_FLASH64MC, RID_WILDCARD,
956                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
957                 NO_INQUIRY
958         },
959         {USB_VENDOR_YANO, USB_PRODUCT_YANO_FW800HD, RID_WILDCARD,
960                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
961                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
962         },
963         {USB_VENDOR_YANO, USB_PRODUCT_YANO_U640MO, RID_WILDCARD,
964                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
965                 FORCE_SHORT_INQUIRY
966         },
967         {USB_VENDOR_YEDATA, USB_PRODUCT_YEDATA_FLASHBUSTERU, RID_WILDCARD,
968                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
969                 NO_GETMAXLUN
970         },
971         {USB_VENDOR_ZORAN, USB_PRODUCT_ZORAN_EX20DSC, RID_WILDCARD,
972                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
973                 NO_QUIRKS
974         },
975         {USB_VENDOR_MEIZU, USB_PRODUCT_MEIZU_M6_SL, RID_WILDCARD,
976                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
977                 NO_INQUIRY | NO_SYNCHRONIZE_CACHE
978         },
979         {USB_VENDOR_ACTIONS, USB_PRODUCT_ACTIONS_MP4, RID_WILDCARD,
980                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
981                 NO_SYNCHRONIZE_CACHE
982         },
983         {USB_VENDOR_ASUS, USB_PRODUCT_ASUS_GMSC, RID_WILDCARD,
984                 UMASS_PROTO_DEFAULT,
985                 NO_SYNCHRONIZE_CACHE
986         },
987         {VID_EOT, PID_EOT, RID_EOT, 0, 0}
988 };
989
990 struct umass_softc {
991
992         struct scsi_sense cam_scsi_sense;
993         struct scsi_test_unit_ready cam_scsi_test_unit_ready;
994         struct mtx sc_mtx;
995         struct {
996                 uint8_t *data_ptr;
997                 union ccb *ccb;
998                 umass_callback_t *callback;
999
1000                 uint32_t data_len;      /* bytes */
1001                 uint32_t data_rem;      /* bytes */
1002                 uint32_t data_timeout;  /* ms */
1003                 uint32_t actlen;        /* bytes */
1004
1005                 uint8_t cmd_data[UMASS_MAX_CMDLEN];
1006                 uint8_t cmd_len;        /* bytes */
1007                 uint8_t dir;
1008                 uint8_t lun;
1009         }       sc_transfer;
1010
1011         /* Bulk specific variables for transfers in progress */
1012         umass_bbb_cbw_t cbw;            /* command block wrapper */
1013         umass_bbb_csw_t csw;            /* command status wrapper */
1014
1015         /* CBI specific variables for transfers in progress */
1016         umass_cbi_sbl_t sbl;            /* status block */
1017
1018         device_t sc_dev;
1019         struct usb_device *sc_udev;
1020         struct cam_sim *sc_sim;         /* SCSI Interface Module */
1021         struct usb_xfer *sc_xfer[UMASS_T_MAX];
1022
1023         /*
1024          * The command transform function is used to convert the SCSI
1025          * commands into their derivatives, like UFI, ATAPI, and friends.
1026          */
1027         umass_transform_t *sc_transform;
1028
1029         uint32_t sc_unit;
1030
1031         uint16_t sc_proto;              /* wire and cmd protocol */
1032         uint16_t sc_quirks;             /* they got it almost right */
1033
1034         uint8_t sc_name[16];
1035         uint8_t sc_iface_no;            /* interface number */
1036         uint8_t sc_maxlun;              /* maximum LUN number, inclusive */
1037         uint8_t sc_last_xfer_index;
1038         uint8_t sc_status_try;
1039 };
1040
1041 struct umass_probe_proto {
1042         uint16_t quirks;
1043         uint16_t proto;
1044
1045         int32_t error;
1046 };
1047
1048 /* prototypes */
1049
1050 static device_probe_t umass_probe;
1051 static device_attach_t umass_attach;
1052 static device_detach_t umass_detach;
1053
1054 static usb_callback_t umass_tr_error;
1055 static usb_callback_t umass_t_bbb_reset1_callback;
1056 static usb_callback_t umass_t_bbb_reset2_callback;
1057 static usb_callback_t umass_t_bbb_reset3_callback;
1058 static usb_callback_t umass_t_bbb_command_callback;
1059 static usb_callback_t umass_t_bbb_data_read_callback;
1060 static usb_callback_t umass_t_bbb_data_rd_cs_callback;
1061 static usb_callback_t umass_t_bbb_data_write_callback;
1062 static usb_callback_t umass_t_bbb_data_wr_cs_callback;
1063 static usb_callback_t umass_t_bbb_status_callback;
1064 static usb_callback_t umass_t_cbi_reset1_callback;
1065 static usb_callback_t umass_t_cbi_reset2_callback;
1066 static usb_callback_t umass_t_cbi_reset3_callback;
1067 static usb_callback_t umass_t_cbi_reset4_callback;
1068 static usb_callback_t umass_t_cbi_command_callback;
1069 static usb_callback_t umass_t_cbi_data_read_callback;
1070 static usb_callback_t umass_t_cbi_data_rd_cs_callback;
1071 static usb_callback_t umass_t_cbi_data_write_callback;
1072 static usb_callback_t umass_t_cbi_data_wr_cs_callback;
1073 static usb_callback_t umass_t_cbi_status_callback;
1074
1075 static void     umass_cancel_ccb(struct umass_softc *);
1076 static void     umass_init_shuttle(struct umass_softc *);
1077 static void     umass_reset(struct umass_softc *);
1078 static void     umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
1079                     uint8_t, uint8_t, usb_error_t);
1080 static void     umass_command_start(struct umass_softc *, uint8_t, void *,
1081                     uint32_t, uint32_t, umass_callback_t *, union ccb *);
1082 static uint8_t  umass_bbb_get_max_lun(struct umass_softc *);
1083 static void     umass_cbi_start_status(struct umass_softc *);
1084 static void     umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
1085                     uint8_t, uint8_t, usb_error_t);
1086 static int      umass_cam_attach_sim(struct umass_softc *);
1087 static void     umass_cam_rescan_callback(struct cam_periph *, union ccb *);
1088 static void     umass_cam_rescan(struct umass_softc *);
1089 static void     umass_cam_attach(struct umass_softc *);
1090 static void     umass_cam_detach_sim(struct umass_softc *);
1091 static void     umass_cam_action(struct cam_sim *, union ccb *);
1092 static void     umass_cam_poll(struct cam_sim *);
1093 static void     umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
1094                     uint8_t);
1095 static void     umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
1096                     uint8_t);
1097 static void     umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
1098                     uint8_t);
1099 static uint8_t  umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
1100 static uint8_t  umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
1101 static uint8_t  umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
1102 static uint8_t  umass_atapi_transform(struct umass_softc *, uint8_t *,
1103                     uint8_t);
1104 static uint8_t  umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
1105 static uint8_t  umass_std_transform(struct umass_softc *, union ccb *, uint8_t
1106                     *, uint8_t);
1107
1108 #if USB_DEBUG
1109 static void     umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
1110 static void     umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
1111 static void     umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
1112 static void     umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
1113                     uint32_t);
1114 #endif
1115
1116 static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
1117
1118         [UMASS_T_BBB_RESET1] = {
1119                 .type = UE_CONTROL,
1120                 .endpoint = 0x00,       /* Control pipe */
1121                 .direction = UE_DIR_ANY,
1122                 .bufsize = sizeof(struct usb_device_request),
1123                 .callback = &umass_t_bbb_reset1_callback,
1124                 .timeout = 5000,        /* 5 seconds */
1125                 .interval = 500,        /* 500 milliseconds */
1126         },
1127
1128         [UMASS_T_BBB_RESET2] = {
1129                 .type = UE_CONTROL,
1130                 .endpoint = 0x00,       /* Control pipe */
1131                 .direction = UE_DIR_ANY,
1132                 .bufsize = sizeof(struct usb_device_request),
1133                 .callback = &umass_t_bbb_reset2_callback,
1134                 .timeout = 5000,        /* 5 seconds */
1135                 .interval = 50, /* 50 milliseconds */
1136         },
1137
1138         [UMASS_T_BBB_RESET3] = {
1139                 .type = UE_CONTROL,
1140                 .endpoint = 0x00,       /* Control pipe */
1141                 .direction = UE_DIR_ANY,
1142                 .bufsize = sizeof(struct usb_device_request),
1143                 .callback = &umass_t_bbb_reset3_callback,
1144                 .timeout = 5000,        /* 5 seconds */
1145                 .interval = 50, /* 50 milliseconds */
1146         },
1147
1148         [UMASS_T_BBB_COMMAND] = {
1149                 .type = UE_BULK,
1150                 .endpoint = UE_ADDR_ANY,
1151                 .direction = UE_DIR_OUT,
1152                 .bufsize = sizeof(umass_bbb_cbw_t),
1153                 .callback = &umass_t_bbb_command_callback,
1154                 .timeout = 5000,        /* 5 seconds */
1155         },
1156
1157         [UMASS_T_BBB_DATA_READ] = {
1158                 .type = UE_BULK,
1159                 .endpoint = UE_ADDR_ANY,
1160                 .direction = UE_DIR_IN,
1161                 .bufsize = UMASS_BULK_SIZE,
1162                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1163                 .callback = &umass_t_bbb_data_read_callback,
1164                 .timeout = 0,   /* overwritten later */
1165         },
1166
1167         [UMASS_T_BBB_DATA_RD_CS] = {
1168                 .type = UE_CONTROL,
1169                 .endpoint = 0x00,       /* Control pipe */
1170                 .direction = UE_DIR_ANY,
1171                 .bufsize = sizeof(struct usb_device_request),
1172                 .callback = &umass_t_bbb_data_rd_cs_callback,
1173                 .timeout = 5000,        /* 5 seconds */
1174         },
1175
1176         [UMASS_T_BBB_DATA_WRITE] = {
1177                 .type = UE_BULK,
1178                 .endpoint = UE_ADDR_ANY,
1179                 .direction = UE_DIR_OUT,
1180                 .bufsize = UMASS_BULK_SIZE,
1181                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1182                 .callback = &umass_t_bbb_data_write_callback,
1183                 .timeout = 0,   /* overwritten later */
1184         },
1185
1186         [UMASS_T_BBB_DATA_WR_CS] = {
1187                 .type = UE_CONTROL,
1188                 .endpoint = 0x00,       /* Control pipe */
1189                 .direction = UE_DIR_ANY,
1190                 .bufsize = sizeof(struct usb_device_request),
1191                 .callback = &umass_t_bbb_data_wr_cs_callback,
1192                 .timeout = 5000,        /* 5 seconds */
1193         },
1194
1195         [UMASS_T_BBB_STATUS] = {
1196                 .type = UE_BULK,
1197                 .endpoint = UE_ADDR_ANY,
1198                 .direction = UE_DIR_IN,
1199                 .bufsize = sizeof(umass_bbb_csw_t),
1200                 .flags = {.short_xfer_ok = 1,},
1201                 .callback = &umass_t_bbb_status_callback,
1202                 .timeout = 5000,        /* ms */
1203         },
1204 };
1205
1206 static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
1207
1208         [UMASS_T_CBI_RESET1] = {
1209                 .type = UE_CONTROL,
1210                 .endpoint = 0x00,       /* Control pipe */
1211                 .direction = UE_DIR_ANY,
1212                 .bufsize = (sizeof(struct usb_device_request) +
1213                     UMASS_CBI_DIAGNOSTIC_CMDLEN),
1214                 .callback = &umass_t_cbi_reset1_callback,
1215                 .timeout = 5000,        /* 5 seconds */
1216                 .interval = 500,        /* 500 milliseconds */
1217         },
1218
1219         [UMASS_T_CBI_RESET2] = {
1220                 .type = UE_CONTROL,
1221                 .endpoint = 0x00,       /* Control pipe */
1222                 .direction = UE_DIR_ANY,
1223                 .bufsize = sizeof(struct usb_device_request),
1224                 .callback = &umass_t_cbi_reset2_callback,
1225                 .timeout = 5000,        /* 5 seconds */
1226                 .interval = 50, /* 50 milliseconds */
1227         },
1228
1229         [UMASS_T_CBI_RESET3] = {
1230                 .type = UE_CONTROL,
1231                 .endpoint = 0x00,       /* Control pipe */
1232                 .direction = UE_DIR_ANY,
1233                 .bufsize = sizeof(struct usb_device_request),
1234                 .callback = &umass_t_cbi_reset3_callback,
1235                 .timeout = 5000,        /* 5 seconds */
1236                 .interval = 50, /* 50 milliseconds */
1237         },
1238
1239         [UMASS_T_CBI_COMMAND] = {
1240                 .type = UE_CONTROL,
1241                 .endpoint = 0x00,       /* Control pipe */
1242                 .direction = UE_DIR_ANY,
1243                 .bufsize = (sizeof(struct usb_device_request) +
1244                     UMASS_MAX_CMDLEN),
1245                 .callback = &umass_t_cbi_command_callback,
1246                 .timeout = 5000,        /* 5 seconds */
1247         },
1248
1249         [UMASS_T_CBI_DATA_READ] = {
1250                 .type = UE_BULK,
1251                 .endpoint = UE_ADDR_ANY,
1252                 .direction = UE_DIR_IN,
1253                 .bufsize = UMASS_BULK_SIZE,
1254                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1255                 .callback = &umass_t_cbi_data_read_callback,
1256                 .timeout = 0,   /* overwritten later */
1257         },
1258
1259         [UMASS_T_CBI_DATA_RD_CS] = {
1260                 .type = UE_CONTROL,
1261                 .endpoint = 0x00,       /* Control pipe */
1262                 .direction = UE_DIR_ANY,
1263                 .bufsize = sizeof(struct usb_device_request),
1264                 .callback = &umass_t_cbi_data_rd_cs_callback,
1265                 .timeout = 5000,        /* 5 seconds */
1266         },
1267
1268         [UMASS_T_CBI_DATA_WRITE] = {
1269                 .type = UE_BULK,
1270                 .endpoint = UE_ADDR_ANY,
1271                 .direction = UE_DIR_OUT,
1272                 .bufsize = UMASS_BULK_SIZE,
1273                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1274                 .callback = &umass_t_cbi_data_write_callback,
1275                 .timeout = 0,   /* overwritten later */
1276         },
1277
1278         [UMASS_T_CBI_DATA_WR_CS] = {
1279                 .type = UE_CONTROL,
1280                 .endpoint = 0x00,       /* Control pipe */
1281                 .direction = UE_DIR_ANY,
1282                 .bufsize = sizeof(struct usb_device_request),
1283                 .callback = &umass_t_cbi_data_wr_cs_callback,
1284                 .timeout = 5000,        /* 5 seconds */
1285         },
1286
1287         [UMASS_T_CBI_STATUS] = {
1288                 .type = UE_INTERRUPT,
1289                 .endpoint = UE_ADDR_ANY,
1290                 .direction = UE_DIR_IN,
1291                 .flags = {.short_xfer_ok = 1,},
1292                 .bufsize = sizeof(umass_cbi_sbl_t),
1293                 .callback = &umass_t_cbi_status_callback,
1294                 .timeout = 5000,        /* ms */
1295         },
1296
1297         [UMASS_T_CBI_RESET4] = {
1298                 .type = UE_CONTROL,
1299                 .endpoint = 0x00,       /* Control pipe */
1300                 .direction = UE_DIR_ANY,
1301                 .bufsize = sizeof(struct usb_device_request),
1302                 .callback = &umass_t_cbi_reset4_callback,
1303                 .timeout = 5000,        /* ms */
1304         },
1305 };
1306
1307 /* If device cannot return valid inquiry data, fake it */
1308 static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1309         0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
1310          /* additional_length */ 31, 0, 0, 0
1311 };
1312
1313 #define UFI_COMMAND_LENGTH      12      /* UFI commands are always 12 bytes */
1314 #define ATAPI_COMMAND_LENGTH    12      /* ATAPI commands are always 12 bytes */
1315
1316 static devclass_t umass_devclass;
1317
1318 static device_method_t umass_methods[] = {
1319         /* Device interface */
1320         DEVMETHOD(device_probe, umass_probe),
1321         DEVMETHOD(device_attach, umass_attach),
1322         DEVMETHOD(device_detach, umass_detach),
1323         {0, 0}
1324 };
1325
1326 static driver_t umass_driver = {
1327         .name = "umass",
1328         .methods = umass_methods,
1329         .size = sizeof(struct umass_softc),
1330 };
1331
1332 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
1333 MODULE_DEPEND(umass, usb, 1, 1, 1);
1334 MODULE_DEPEND(umass, cam, 1, 1, 1);
1335
1336 /*
1337  * USB device probe/attach/detach
1338  */
1339
1340 static uint16_t
1341 umass_get_proto(struct usb_interface *iface)
1342 {
1343         struct usb_interface_descriptor *id;
1344         uint16_t retval;
1345
1346         retval = 0;
1347
1348         /* Check for a standards compliant device */
1349         id = usbd_get_interface_descriptor(iface);
1350         if ((id == NULL) ||
1351             (id->bInterfaceClass != UICLASS_MASS)) {
1352                 goto done;
1353         }
1354         switch (id->bInterfaceSubClass) {
1355         case UISUBCLASS_SCSI:
1356                 retval |= UMASS_PROTO_SCSI;
1357                 break;
1358         case UISUBCLASS_UFI:
1359                 retval |= UMASS_PROTO_UFI;
1360                 break;
1361         case UISUBCLASS_RBC:
1362                 retval |= UMASS_PROTO_RBC;
1363                 break;
1364         case UISUBCLASS_SFF8020I:
1365         case UISUBCLASS_SFF8070I:
1366                 retval |= UMASS_PROTO_ATAPI;
1367                 break;
1368         default:
1369                 retval = 0;
1370                 goto done;
1371         }
1372
1373         switch (id->bInterfaceProtocol) {
1374         case UIPROTO_MASS_CBI:
1375                 retval |= UMASS_PROTO_CBI;
1376                 break;
1377         case UIPROTO_MASS_CBI_I:
1378                 retval |= UMASS_PROTO_CBI_I;
1379                 break;
1380         case UIPROTO_MASS_BBB_OLD:
1381         case UIPROTO_MASS_BBB:
1382                 retval |= UMASS_PROTO_BBB;
1383                 break;
1384         default:
1385                 retval = 0;
1386                 goto done;
1387         }
1388 done:
1389         return (retval);
1390 }
1391
1392 /*
1393  * Match the device we are seeing with the
1394  * devices supported.
1395  */
1396 static struct umass_probe_proto
1397 umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
1398 {
1399         const struct umass_devdescr *udd = umass_devdescr;
1400         struct umass_probe_proto ret;
1401
1402         memset(&ret, 0, sizeof(ret));
1403
1404         /*
1405          * An entry specifically for Y-E Data devices as they don't fit in
1406          * the device description table.
1407          */
1408         if ((uaa->info.idVendor == USB_VENDOR_YEDATA) &&
1409             (uaa->info.idProduct == USB_PRODUCT_YEDATA_FLASHBUSTERU)) {
1410
1411                 /*
1412                  * Revisions < 1.28 do not handle the interrupt endpoint
1413                  * very well.
1414                  */
1415                 if (uaa->info.bcdDevice < 0x128) {
1416                         ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1417                 } else {
1418                         ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1419                 }
1420
1421                 /*
1422                  * Revisions < 1.28 do not have the TEST UNIT READY command
1423                  * Revisions == 1.28 have a broken TEST UNIT READY
1424                  */
1425                 if (uaa->info.bcdDevice <= 0x128) {
1426                         ret.quirks |= NO_TEST_UNIT_READY;
1427                 }
1428                 ret.quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1429                 goto done;
1430         }
1431         /*
1432          * Check the list of supported devices for a match. While looking,
1433          * check for wildcarded and fully matched. First match wins.
1434          */
1435         for (; udd->vid != VID_EOT; udd++) {
1436                 if (((udd->vid == uaa->info.idVendor) ||
1437                     (udd->vid == VID_WILDCARD)) &&
1438                     ((udd->pid == uaa->info.idProduct) ||
1439                     (udd->pid == PID_WILDCARD))) {
1440                         if (udd->rid == RID_WILDCARD) {
1441                                 ret.proto = udd->proto;
1442                                 ret.quirks = udd->quirks;
1443                                 if (ret.proto == UMASS_PROTO_DEFAULT)
1444                                         goto default_proto;
1445                                 else
1446                                         goto done;
1447                         } else if (udd->rid == uaa->info.bcdDevice) {
1448                                 ret.proto = udd->proto;
1449                                 ret.quirks = udd->quirks;
1450                                 if (ret.proto == UMASS_PROTO_DEFAULT)
1451                                         goto default_proto;
1452                                 else
1453                                         goto done;
1454                         }               /* else RID does not match */
1455                 }
1456         }
1457
1458 default_proto:
1459         ret.proto = umass_get_proto(uaa->iface);
1460         if (ret.proto == 0)
1461                 ret.error = ENXIO;
1462         else
1463                 ret.error = 0;
1464 done:
1465         return (ret);
1466 }
1467
1468 static int
1469 umass_probe(device_t dev)
1470 {
1471         struct usb_attach_arg *uaa = device_get_ivars(dev);
1472         struct umass_probe_proto temp;
1473
1474         if (uaa->usb_mode != USB_MODE_HOST) {
1475                 return (ENXIO);
1476         }
1477         if (uaa->use_generic == 0) {
1478                 /* give other drivers a try first */
1479                 return (ENXIO);
1480         }
1481         temp = umass_probe_proto(dev, uaa);
1482
1483         return (temp.error);
1484 }
1485
1486 static int
1487 umass_attach(device_t dev)
1488 {
1489         struct umass_softc *sc = device_get_softc(dev);
1490         struct usb_attach_arg *uaa = device_get_ivars(dev);
1491         struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
1492         struct usb_interface_descriptor *id;
1493         int32_t err;
1494
1495         /*
1496          * NOTE: the softc struct is bzero-ed in device_set_driver.
1497          * We can safely call umass_detach without specifically
1498          * initializing the struct.
1499          */
1500
1501         sc->sc_dev = dev;
1502         sc->sc_udev = uaa->device;
1503         sc->sc_proto = temp.proto;
1504         sc->sc_quirks = temp.quirks;
1505         sc->sc_unit = device_get_unit(dev);
1506
1507         snprintf(sc->sc_name, sizeof(sc->sc_name),
1508             "%s", device_get_nameunit(dev));
1509
1510         device_set_usb_desc(dev);
1511
1512         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), 
1513             NULL, MTX_DEF | MTX_RECURSE);
1514
1515         /* get interface index */
1516
1517         id = usbd_get_interface_descriptor(uaa->iface);
1518         if (id == NULL) {
1519                 device_printf(dev, "failed to get "
1520                     "interface number\n");
1521                 goto detach;
1522         }
1523         sc->sc_iface_no = id->bInterfaceNumber;
1524
1525 #if USB_DEBUG
1526         device_printf(dev, " ");
1527
1528         switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
1529         case UMASS_PROTO_SCSI:
1530                 printf("SCSI");
1531                 break;
1532         case UMASS_PROTO_ATAPI:
1533                 printf("8070i (ATAPI)");
1534                 break;
1535         case UMASS_PROTO_UFI:
1536                 printf("UFI");
1537                 break;
1538         case UMASS_PROTO_RBC:
1539                 printf("RBC");
1540                 break;
1541         default:
1542                 printf("(unknown 0x%02x)",
1543                     sc->sc_proto & UMASS_PROTO_COMMAND);
1544                 break;
1545         }
1546
1547         printf(" over ");
1548
1549         switch (sc->sc_proto & UMASS_PROTO_WIRE) {
1550         case UMASS_PROTO_BBB:
1551                 printf("Bulk-Only");
1552                 break;
1553         case UMASS_PROTO_CBI:           /* uses Comand/Bulk pipes */
1554                 printf("CBI");
1555                 break;
1556         case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1557                 printf("CBI with CCI");
1558                 break;
1559         default:
1560                 printf("(unknown 0x%02x)",
1561                     sc->sc_proto & UMASS_PROTO_WIRE);
1562         }
1563
1564         printf("; quirks = 0x%04x\n", sc->sc_quirks);
1565 #endif
1566
1567         if (sc->sc_quirks & ALT_IFACE_1) {
1568                 err = usbd_set_alt_interface_index
1569                     (uaa->device, uaa->info.bIfaceIndex, 1);
1570
1571                 if (err) {
1572                         DPRINTF(sc, UDMASS_USB, "could not switch to "
1573                             "Alt Interface 1\n");
1574                         goto detach;
1575                 }
1576         }
1577         /* allocate all required USB transfers */
1578
1579         if (sc->sc_proto & UMASS_PROTO_BBB) {
1580
1581                 err = usbd_transfer_setup(uaa->device,
1582                     &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
1583                     UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
1584
1585                 /* skip reset first time */
1586                 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1587
1588         } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
1589
1590                 err = usbd_transfer_setup(uaa->device,
1591                     &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
1592                     (sc->sc_proto & UMASS_PROTO_CBI_I) ?
1593                     UMASS_T_CBI_MAX : (UMASS_T_CBI_MAX - 2), sc,
1594                     &sc->sc_mtx);
1595
1596                 /* skip reset first time */
1597                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1598
1599         } else {
1600                 err = USB_ERR_INVAL;
1601         }
1602
1603         if (err) {
1604                 device_printf(dev, "could not setup required "
1605                     "transfers, %s\n", usbd_errstr(err));
1606                 goto detach;
1607         }
1608         sc->sc_transform =
1609             (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1610             (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1611             (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1612             (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1613             &umass_no_transform;
1614
1615         /* from here onwards the device can be used. */
1616
1617         if (sc->sc_quirks & SHUTTLE_INIT) {
1618                 umass_init_shuttle(sc);
1619         }
1620         /* get the maximum LUN supported by the device */
1621
1622         if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1623             !(sc->sc_quirks & NO_GETMAXLUN))
1624                 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1625         else
1626                 sc->sc_maxlun = 0;
1627
1628         /* Prepare the SCSI command block */
1629         sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1630         sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1631
1632         /*
1633          * some devices need a delay after that the configuration value is
1634          * set to function properly:
1635          */
1636         usb_pause_mtx(NULL, hz);
1637
1638         /* register the SIM */
1639         err = umass_cam_attach_sim(sc);
1640         if (err) {
1641                 goto detach;
1642         }
1643         /* scan the SIM */
1644         umass_cam_attach(sc);
1645
1646         DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1647
1648         return (0);                     /* success */
1649
1650 detach:
1651         umass_detach(dev);
1652         return (ENXIO);                 /* failure */
1653 }
1654
1655 static int
1656 umass_detach(device_t dev)
1657 {
1658         struct umass_softc *sc = device_get_softc(dev);
1659
1660         DPRINTF(sc, UDMASS_USB, "\n");
1661
1662         /* teardown our statemachine */
1663
1664         usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1665
1666 #if (__FreeBSD_version >= 700037)
1667         mtx_lock(&sc->sc_mtx);
1668 #endif
1669         umass_cam_detach_sim(sc);
1670
1671 #if (__FreeBSD_version >= 700037)
1672         mtx_unlock(&sc->sc_mtx);
1673 #endif
1674         mtx_destroy(&sc->sc_mtx);
1675
1676         return (0);                     /* success */
1677 }
1678
1679 static void
1680 umass_init_shuttle(struct umass_softc *sc)
1681 {
1682         struct usb_device_request req;
1683         usb_error_t err;
1684         uint8_t status[2] = {0, 0};
1685
1686         /*
1687          * The Linux driver does this, but no one can tell us what the
1688          * command does.
1689          */
1690         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1691         req.bRequest = 1;               /* XXX unknown command */
1692         USETW(req.wValue, 0);
1693         req.wIndex[0] = sc->sc_iface_no;
1694         req.wIndex[1] = 0;
1695         USETW(req.wLength, sizeof(status));
1696         err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1697
1698         DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1699             status[0], status[1]);
1700 }
1701
1702 /*
1703  * Generic functions to handle transfers
1704  */
1705
1706 static void
1707 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1708 {
1709         DPRINTF(sc, UDMASS_GEN, "transfer index = "
1710             "%d\n", xfer_index);
1711
1712         if (sc->sc_xfer[xfer_index]) {
1713                 sc->sc_last_xfer_index = xfer_index;
1714                 usbd_transfer_start(sc->sc_xfer[xfer_index]);
1715         } else {
1716                 umass_cancel_ccb(sc);
1717         }
1718 }
1719
1720 static void
1721 umass_reset(struct umass_softc *sc)
1722 {
1723         DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1724
1725         /*
1726          * stop the last transfer, if not already stopped:
1727          */
1728         usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1729         umass_transfer_start(sc, 0);
1730 }
1731
1732 static void
1733 umass_cancel_ccb(struct umass_softc *sc)
1734 {
1735         union ccb *ccb;
1736
1737         mtx_assert(&sc->sc_mtx, MA_OWNED);
1738
1739         ccb = sc->sc_transfer.ccb;
1740         sc->sc_transfer.ccb = NULL;
1741         sc->sc_last_xfer_index = 0;
1742
1743         if (ccb) {
1744                 (sc->sc_transfer.callback)
1745                     (sc, ccb, (sc->sc_transfer.data_len -
1746                     sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1747         }
1748 }
1749
1750 static void
1751 umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1752 {
1753         struct umass_softc *sc = usbd_xfer_softc(xfer);
1754
1755         if (error != USB_ERR_CANCELLED) {
1756
1757                 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1758                     "reset\n", usbd_errstr(error));
1759         }
1760         umass_cancel_ccb(sc);
1761 }
1762
1763 /*
1764  * BBB protocol specific functions
1765  */
1766
1767 static void
1768 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1769 {
1770         struct umass_softc *sc = usbd_xfer_softc(xfer);
1771         struct usb_device_request req;
1772         struct usb_page_cache *pc;
1773
1774         switch (USB_GET_STATE(xfer)) {
1775         case USB_ST_TRANSFERRED:
1776                 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1777                 return;
1778
1779         case USB_ST_SETUP:
1780                 /*
1781                  * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1782                  *
1783                  * For Reset Recovery the host shall issue in the following order:
1784                  * a) a Bulk-Only Mass Storage Reset
1785                  * b) a Clear Feature HALT to the Bulk-In endpoint
1786                  * c) a Clear Feature HALT to the Bulk-Out endpoint
1787                  *
1788                  * This is done in 3 steps, using 3 transfers:
1789                  * UMASS_T_BBB_RESET1
1790                  * UMASS_T_BBB_RESET2
1791                  * UMASS_T_BBB_RESET3
1792                  */
1793
1794                 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1795
1796                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1797                 req.bRequest = UR_BBB_RESET;    /* bulk only reset */
1798                 USETW(req.wValue, 0);
1799                 req.wIndex[0] = sc->sc_iface_no;
1800                 req.wIndex[1] = 0;
1801                 USETW(req.wLength, 0);
1802
1803                 pc = usbd_xfer_get_frame(xfer, 0);
1804                 usbd_copy_in(pc, 0, &req, sizeof(req));
1805
1806                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1807                 usbd_xfer_set_frames(xfer, 1);
1808                 usbd_transfer_submit(xfer);
1809                 return;
1810
1811         default:                        /* Error */
1812                 umass_tr_error(xfer, error);
1813                 return;
1814
1815         }
1816 }
1817
1818 static void
1819 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1820 {
1821         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1822             UMASS_T_BBB_DATA_READ, error);
1823 }
1824
1825 static void
1826 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1827 {
1828         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1829             UMASS_T_BBB_DATA_WRITE, error);
1830 }
1831
1832 static void
1833 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1834     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1835 {
1836         struct umass_softc *sc = usbd_xfer_softc(xfer);
1837
1838         switch (USB_GET_STATE(xfer)) {
1839         case USB_ST_TRANSFERRED:
1840 tr_transferred:
1841                 umass_transfer_start(sc, next_xfer);
1842                 return;
1843
1844         case USB_ST_SETUP:
1845                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1846                         goto tr_transferred;
1847                 }
1848                 return;
1849
1850         default:                        /* Error */
1851                 umass_tr_error(xfer, error);
1852                 return;
1853
1854         }
1855 }
1856
1857 static void
1858 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1859 {
1860         struct umass_softc *sc = usbd_xfer_softc(xfer);
1861         union ccb *ccb = sc->sc_transfer.ccb;
1862         struct usb_page_cache *pc;
1863         uint32_t tag;
1864
1865         switch (USB_GET_STATE(xfer)) {
1866         case USB_ST_TRANSFERRED:
1867                 umass_transfer_start
1868                     (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1869                     (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1870                     UMASS_T_BBB_STATUS));
1871                 return;
1872
1873         case USB_ST_SETUP:
1874
1875                 sc->sc_status_try = 0;
1876
1877                 if (ccb) {
1878
1879                         /*
1880                          * the initial value is not important,
1881                          * as long as the values are unique:
1882                          */
1883                         tag = UGETDW(sc->cbw.dCBWTag) + 1;
1884
1885                         USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1886                         USETDW(sc->cbw.dCBWTag, tag);
1887
1888                         /*
1889                          * dCBWDataTransferLength:
1890                          *   This field indicates the number of bytes of data that the host
1891                          *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1892                          *   the Direction bit) during the execution of this command. If this
1893                          *   field is set to 0, the device will expect that no data will be
1894                          *   transferred IN or OUT during this command, regardless of the value
1895                          *   of the Direction bit defined in dCBWFlags.
1896                          */
1897                         USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1898
1899                         /*
1900                          * dCBWFlags:
1901                          *   The bits of the Flags field are defined as follows:
1902                          *     Bits 0-6  reserved
1903                          *     Bit  7    Direction - this bit shall be ignored if the
1904                          *                           dCBWDataTransferLength field is zero.
1905                          *               0 = data Out from host to device
1906                          *               1 = data In from device to host
1907                          */
1908                         sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1909                             CBWFLAGS_IN : CBWFLAGS_OUT);
1910                         sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1911
1912                         if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1913                                 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1914                                 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1915                         }
1916                         sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1917
1918                         bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB,
1919                             sc->sc_transfer.cmd_len);
1920
1921                         bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len,
1922                             sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len);
1923
1924                         DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1925
1926                         pc = usbd_xfer_get_frame(xfer, 0);
1927                         usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1928                         usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1929
1930                         usbd_transfer_submit(xfer);
1931                 }
1932                 return;
1933
1934         default:                        /* Error */
1935                 umass_tr_error(xfer, error);
1936                 return;
1937
1938         }
1939 }
1940
1941 static void
1942 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1943 {
1944         struct umass_softc *sc = usbd_xfer_softc(xfer);
1945         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1946 #ifndef UMASS_EXT_BUFFER
1947         struct usb_page_cache *pc;
1948 #endif
1949         int actlen, sumlen;
1950
1951         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1952
1953         switch (USB_GET_STATE(xfer)) {
1954         case USB_ST_TRANSFERRED:
1955 #ifndef UMASS_EXT_BUFFER
1956                 pc = usbd_xfer_get_frame(xfer, 0);
1957                 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1958 #endif
1959                 sc->sc_transfer.data_rem -= actlen;
1960                 sc->sc_transfer.data_ptr += actlen;
1961                 sc->sc_transfer.actlen += actlen;
1962
1963                 if (actlen < sumlen) {
1964                         /* short transfer */
1965                         sc->sc_transfer.data_rem = 0;
1966                 }
1967         case USB_ST_SETUP:
1968                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1969                     max_bulk, sc->sc_transfer.data_rem);
1970
1971                 if (sc->sc_transfer.data_rem == 0) {
1972                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1973                         return;
1974                 }
1975                 if (max_bulk > sc->sc_transfer.data_rem) {
1976                         max_bulk = sc->sc_transfer.data_rem;
1977                 }
1978                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1979
1980 #ifdef UMASS_EXT_BUFFER
1981                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1982                     max_bulk);
1983 #else
1984                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1985 #endif
1986                 usbd_transfer_submit(xfer);
1987                 return;
1988
1989         default:                        /* Error */
1990                 if (error == USB_ERR_CANCELLED) {
1991                         umass_tr_error(xfer, error);
1992                 } else {
1993                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1994                 }
1995                 return;
1996
1997         }
1998 }
1999
2000 static void
2001 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2002 {
2003         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
2004             UMASS_T_BBB_DATA_READ, error);
2005 }
2006
2007 static void
2008 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
2009 {
2010         struct umass_softc *sc = usbd_xfer_softc(xfer);
2011         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2012 #ifndef UMASS_EXT_BUFFER
2013         struct usb_page_cache *pc;
2014 #endif
2015         int actlen, sumlen;
2016
2017         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2018
2019         switch (USB_GET_STATE(xfer)) {
2020         case USB_ST_TRANSFERRED:
2021                 sc->sc_transfer.data_rem -= actlen;
2022                 sc->sc_transfer.data_ptr += actlen;
2023                 sc->sc_transfer.actlen += actlen;
2024
2025                 if (actlen < sumlen) {
2026                         /* short transfer */
2027                         sc->sc_transfer.data_rem = 0;
2028                 }
2029         case USB_ST_SETUP:
2030                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
2031                     max_bulk, sc->sc_transfer.data_rem);
2032
2033                 if (sc->sc_transfer.data_rem == 0) {
2034                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
2035                         return;
2036                 }
2037                 if (max_bulk > sc->sc_transfer.data_rem) {
2038                         max_bulk = sc->sc_transfer.data_rem;
2039                 }
2040                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2041
2042 #ifdef UMASS_EXT_BUFFER
2043                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2044                     max_bulk);
2045 #else
2046                 pc = usbd_xfer_get_frame(xfer, 0);
2047                 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
2048                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2049 #endif
2050
2051                 usbd_transfer_submit(xfer);
2052                 return;
2053
2054         default:                        /* Error */
2055                 if (error == USB_ERR_CANCELLED) {
2056                         umass_tr_error(xfer, error);
2057                 } else {
2058                         umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
2059                 }
2060                 return;
2061
2062         }
2063 }
2064
2065 static void
2066 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2067 {
2068         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
2069             UMASS_T_BBB_DATA_WRITE, error);
2070 }
2071
2072 static void
2073 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
2074 {
2075         struct umass_softc *sc = usbd_xfer_softc(xfer);
2076         union ccb *ccb = sc->sc_transfer.ccb;
2077         struct usb_page_cache *pc;
2078         uint32_t residue;
2079         int actlen;
2080
2081         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2082
2083         switch (USB_GET_STATE(xfer)) {
2084         case USB_ST_TRANSFERRED:
2085
2086                 /*
2087                  * Do a full reset if there is something wrong with the CSW:
2088                  */
2089                 sc->sc_status_try = 1;
2090
2091                 /* Zero missing parts of the CSW: */
2092
2093                 if (actlen < sizeof(sc->csw)) {
2094                         bzero(&sc->csw, sizeof(sc->csw));
2095                 }
2096                 pc = usbd_xfer_get_frame(xfer, 0);
2097                 usbd_copy_out(pc, 0, &sc->csw, actlen);
2098
2099                 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2100
2101                 residue = UGETDW(sc->csw.dCSWDataResidue);
2102
2103                 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
2104                         residue = (sc->sc_transfer.data_len -
2105                             sc->sc_transfer.actlen);
2106                 }
2107                 if (residue > sc->sc_transfer.data_len) {
2108                         DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
2109                             "to %d bytes\n", residue, sc->sc_transfer.data_len);
2110                         residue = sc->sc_transfer.data_len;
2111                 }
2112                 /* translate weird command-status signatures: */
2113                 if (sc->sc_quirks & WRONG_CSWSIG) {
2114
2115                         uint32_t temp = UGETDW(sc->csw.dCSWSignature);
2116
2117                         if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
2118                             (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
2119                                 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2120                         }
2121                 }
2122                 /* check CSW and handle eventual error */
2123                 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2124                         DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
2125                             UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
2126                         /*
2127                          * Invalid CSW: Wrong signature or wrong tag might
2128                          * indicate that we lost synchronization. Reset the
2129                          * device.
2130                          */
2131                         goto tr_error;
2132                 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
2133                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
2134                             "0x%08x\n", UGETDW(sc->csw.dCSWTag),
2135                             UGETDW(sc->cbw.dCBWTag));
2136                         goto tr_error;
2137                 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2138                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
2139                             sc->csw.bCSWStatus, CSWSTATUS_PHASE);
2140                         goto tr_error;
2141                 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2142                         DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
2143                             "%d\n", residue);
2144                         goto tr_error;
2145                 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
2146                         DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
2147                             sc->sc_transfer.actlen, sc->sc_transfer.data_len);
2148                         goto tr_error;
2149                 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2150                         DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
2151                             "%d\n", residue);
2152
2153                         sc->sc_transfer.ccb = NULL;
2154
2155                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2156
2157                         (sc->sc_transfer.callback)
2158                             (sc, ccb, residue, STATUS_CMD_FAILED);
2159                 } else {
2160                         sc->sc_transfer.ccb = NULL;
2161
2162                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2163
2164                         (sc->sc_transfer.callback)
2165                             (sc, ccb, residue, STATUS_CMD_OK);
2166                 }
2167                 return;
2168
2169         case USB_ST_SETUP:
2170                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2171                 usbd_transfer_submit(xfer);
2172                 return;
2173
2174         default:
2175 tr_error:
2176                 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
2177                     usbd_errstr(error), sc->sc_status_try);
2178
2179                 if ((error == USB_ERR_CANCELLED) ||
2180                     (sc->sc_status_try)) {
2181                         umass_tr_error(xfer, error);
2182                 } else {
2183                         sc->sc_status_try = 1;
2184                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
2185                 }
2186                 return;
2187
2188         }
2189 }
2190
2191 static void
2192 umass_command_start(struct umass_softc *sc, uint8_t dir,
2193     void *data_ptr, uint32_t data_len,
2194     uint32_t data_timeout, umass_callback_t *callback,
2195     union ccb *ccb)
2196 {
2197         sc->sc_transfer.lun = ccb->ccb_h.target_lun;
2198
2199         /*
2200          * NOTE: assumes that "sc->sc_transfer.cmd_data" and
2201          * "sc->sc_transfer.cmd_len" has been properly
2202          * initialized.
2203          */
2204
2205         sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
2206         sc->sc_transfer.data_ptr = data_ptr;
2207         sc->sc_transfer.data_len = data_len;
2208         sc->sc_transfer.data_rem = data_len;
2209         sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
2210
2211         sc->sc_transfer.actlen = 0;
2212         sc->sc_transfer.callback = callback;
2213         sc->sc_transfer.ccb = ccb;
2214
2215         if (sc->sc_xfer[sc->sc_last_xfer_index]) {
2216                 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
2217         } else {
2218                 ccb->ccb_h.status = CAM_TID_INVALID;
2219                 xpt_done(ccb);
2220         }
2221 }
2222
2223 static uint8_t
2224 umass_bbb_get_max_lun(struct umass_softc *sc)
2225 {
2226         struct usb_device_request req;
2227         usb_error_t err;
2228         uint8_t buf = 0;
2229
2230         /* The Get Max Lun command is a class-specific request. */
2231         req.bmRequestType = UT_READ_CLASS_INTERFACE;
2232         req.bRequest = UR_BBB_GET_MAX_LUN;
2233         USETW(req.wValue, 0);
2234         req.wIndex[0] = sc->sc_iface_no;
2235         req.wIndex[1] = 0;
2236         USETW(req.wLength, 1);
2237
2238         err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
2239         if (err) {
2240                 buf = 0;
2241
2242                 /* Device doesn't support Get Max Lun request. */
2243                 printf("%s: Get Max Lun not supported (%s)\n",
2244                     sc->sc_name, usbd_errstr(err));
2245         }
2246         return (buf);
2247 }
2248
2249 /*
2250  * Command/Bulk/Interrupt (CBI) specific functions
2251  */
2252
2253 static void
2254 umass_cbi_start_status(struct umass_softc *sc)
2255 {
2256         if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
2257                 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
2258         } else {
2259                 union ccb *ccb = sc->sc_transfer.ccb;
2260
2261                 sc->sc_transfer.ccb = NULL;
2262
2263                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2264
2265                 (sc->sc_transfer.callback)
2266                     (sc, ccb, (sc->sc_transfer.data_len -
2267                     sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
2268         }
2269 }
2270
2271 static void
2272 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
2273 {
2274         struct umass_softc *sc = usbd_xfer_softc(xfer);
2275         struct usb_device_request req;
2276         struct usb_page_cache *pc;
2277         uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
2278
2279         uint8_t i;
2280
2281         switch (USB_GET_STATE(xfer)) {
2282         case USB_ST_TRANSFERRED:
2283                 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
2284                 return;
2285
2286         case USB_ST_SETUP:
2287                 /*
2288                  * Command Block Reset Protocol
2289                  *
2290                  * First send a reset request to the device. Then clear
2291                  * any possibly stalled bulk endpoints.
2292                  *
2293                  * This is done in 3 steps, using 3 transfers:
2294                  * UMASS_T_CBI_RESET1
2295                  * UMASS_T_CBI_RESET2
2296                  * UMASS_T_CBI_RESET3
2297                  * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
2298                  */
2299
2300                 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
2301
2302                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2303                 req.bRequest = UR_CBI_ADSC;
2304                 USETW(req.wValue, 0);
2305                 req.wIndex[0] = sc->sc_iface_no;
2306                 req.wIndex[1] = 0;
2307                 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
2308
2309                 /*
2310                  * The 0x1d code is the SEND DIAGNOSTIC command. To
2311                  * distinguish between the two, the last 10 bytes of the CBL
2312                  * is filled with 0xff (section 2.2 of the CBI
2313                  * specification)
2314                  */
2315                 buf[0] = 0x1d;          /* Command Block Reset */
2316                 buf[1] = 0x04;
2317
2318                 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
2319                         buf[i] = 0xff;
2320                 }
2321
2322                 pc = usbd_xfer_get_frame(xfer, 0);
2323                 usbd_copy_in(pc, 0, &req, sizeof(req));
2324                 pc = usbd_xfer_get_frame(xfer, 1);
2325                 usbd_copy_in(pc, 0, buf, sizeof(buf));
2326
2327                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
2328                 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
2329                 usbd_xfer_set_frames(xfer, 2);
2330                 usbd_transfer_submit(xfer);
2331                 return;
2332
2333         default:                        /* Error */
2334                 umass_tr_error(xfer, error);
2335                 return;
2336
2337         }
2338 }
2339
2340 static void
2341 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
2342 {
2343         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
2344             UMASS_T_CBI_DATA_READ, error);
2345 }
2346
2347 static void
2348 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
2349 {
2350         struct umass_softc *sc = usbd_xfer_softc(xfer);
2351
2352         umass_t_cbi_data_clear_stall_callback
2353             (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
2354             sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
2355             UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
2356             UMASS_T_CBI_DATA_WRITE, error);
2357 }
2358
2359 static void
2360 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
2361 {
2362         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
2363             UMASS_T_CBI_STATUS, error);
2364 }
2365
2366 static void
2367 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
2368     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
2369 {
2370         struct umass_softc *sc = usbd_xfer_softc(xfer);
2371
2372         switch (USB_GET_STATE(xfer)) {
2373         case USB_ST_TRANSFERRED:
2374 tr_transferred:
2375                 if (next_xfer == UMASS_T_CBI_STATUS) {
2376                         umass_cbi_start_status(sc);
2377                 } else {
2378                         umass_transfer_start(sc, next_xfer);
2379                 }
2380                 return;
2381
2382         case USB_ST_SETUP:
2383                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
2384                         goto tr_transferred;    /* should not happen */
2385                 }
2386                 return;
2387
2388         default:                        /* Error */
2389                 umass_tr_error(xfer, error);
2390                 return;
2391
2392         }
2393 }
2394
2395 static void
2396 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
2397 {
2398         struct umass_softc *sc = usbd_xfer_softc(xfer);
2399         union ccb *ccb = sc->sc_transfer.ccb;
2400         struct usb_device_request req;
2401         struct usb_page_cache *pc;
2402
2403         switch (USB_GET_STATE(xfer)) {
2404         case USB_ST_TRANSFERRED:
2405
2406                 if (sc->sc_transfer.dir == DIR_NONE) {
2407                         umass_cbi_start_status(sc);
2408                 } else {
2409                         umass_transfer_start
2410                             (sc, (sc->sc_transfer.dir == DIR_IN) ?
2411                             UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
2412                 }
2413                 return;
2414
2415         case USB_ST_SETUP:
2416
2417                 if (ccb) {
2418
2419                         /*
2420                          * do a CBI transfer with cmd_len bytes from
2421                          * cmd_data, possibly a data phase of data_len
2422                          * bytes from/to the device and finally a status
2423                          * read phase.
2424                          */
2425
2426                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2427                         req.bRequest = UR_CBI_ADSC;
2428                         USETW(req.wValue, 0);
2429                         req.wIndex[0] = sc->sc_iface_no;
2430                         req.wIndex[1] = 0;
2431                         req.wLength[0] = sc->sc_transfer.cmd_len;
2432                         req.wLength[1] = 0;
2433
2434                         pc = usbd_xfer_get_frame(xfer, 0);
2435                         usbd_copy_in(pc, 0, &req, sizeof(req));
2436                         pc = usbd_xfer_get_frame(xfer, 1);
2437                         usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
2438                             sc->sc_transfer.cmd_len);
2439
2440                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
2441                         usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
2442                         usbd_xfer_set_frames(xfer,
2443                             sc->sc_transfer.cmd_len ? 2 : 1);
2444
2445                         DIF(UDMASS_CBI,
2446                             umass_cbi_dump_cmd(sc,
2447                             sc->sc_transfer.cmd_data,
2448                             sc->sc_transfer.cmd_len));
2449
2450                         usbd_transfer_submit(xfer);
2451                 }
2452                 return;
2453
2454         default:                        /* Error */
2455                 umass_tr_error(xfer, error);
2456                 return;
2457
2458         }
2459 }
2460
2461 static void
2462 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
2463 {
2464         struct umass_softc *sc = usbd_xfer_softc(xfer);
2465         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2466 #ifndef UMASS_EXT_BUFFER
2467         struct usb_page_cache *pc;
2468 #endif
2469         int actlen, sumlen;
2470
2471         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2472
2473         switch (USB_GET_STATE(xfer)) {
2474         case USB_ST_TRANSFERRED:
2475 #ifndef UMASS_EXT_BUFFER
2476                 pc = usbd_xfer_get_frame(xfer, 0);
2477                 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
2478 #endif
2479                 sc->sc_transfer.data_rem -= actlen;
2480                 sc->sc_transfer.data_ptr += actlen;
2481                 sc->sc_transfer.actlen += actlen;
2482
2483                 if (actlen < sumlen) {
2484                         /* short transfer */
2485                         sc->sc_transfer.data_rem = 0;
2486                 }
2487         case USB_ST_SETUP:
2488                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2489                     max_bulk, sc->sc_transfer.data_rem);
2490
2491                 if (sc->sc_transfer.data_rem == 0) {
2492                         umass_cbi_start_status(sc);
2493                         return;
2494                 }
2495                 if (max_bulk > sc->sc_transfer.data_rem) {
2496                         max_bulk = sc->sc_transfer.data_rem;
2497                 }
2498                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2499
2500 #ifdef UMASS_EXT_BUFFER
2501                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2502                     max_bulk);
2503 #else
2504                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2505 #endif
2506                 usbd_transfer_submit(xfer);
2507                 return;
2508
2509         default:                        /* Error */
2510                 if ((error == USB_ERR_CANCELLED) ||
2511                     (sc->sc_transfer.callback != &umass_cam_cb)) {
2512                         umass_tr_error(xfer, error);
2513                 } else {
2514                         umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
2515                 }
2516                 return;
2517
2518         }
2519 }
2520
2521 static void
2522 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2523 {
2524         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2525             UMASS_T_CBI_DATA_READ, error);
2526 }
2527
2528 static void
2529 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
2530 {
2531         struct umass_softc *sc = usbd_xfer_softc(xfer);
2532         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2533 #ifndef UMASS_EXT_BUFFER
2534         struct usb_page_cache *pc;
2535 #endif
2536         int actlen, sumlen;
2537
2538         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2539
2540         switch (USB_GET_STATE(xfer)) {
2541         case USB_ST_TRANSFERRED:
2542                 sc->sc_transfer.data_rem -= actlen;
2543                 sc->sc_transfer.data_ptr += actlen;
2544                 sc->sc_transfer.actlen += actlen;
2545
2546                 if (actlen < sumlen) {
2547                         /* short transfer */
2548                         sc->sc_transfer.data_rem = 0;
2549                 }
2550         case USB_ST_SETUP:
2551                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2552                     max_bulk, sc->sc_transfer.data_rem);
2553
2554                 if (sc->sc_transfer.data_rem == 0) {
2555                         umass_cbi_start_status(sc);
2556                         return;
2557                 }
2558                 if (max_bulk > sc->sc_transfer.data_rem) {
2559                         max_bulk = sc->sc_transfer.data_rem;
2560                 }
2561                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2562
2563 #ifdef UMASS_EXT_BUFFER
2564                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2565                     max_bulk);
2566 #else
2567                 pc = usbd_xfer_get_frame(xfer, 0);
2568                 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
2569                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2570 #endif
2571
2572                 usbd_transfer_submit(xfer);
2573                 return;
2574
2575         default:                        /* Error */
2576                 if ((error == USB_ERR_CANCELLED) ||
2577                     (sc->sc_transfer.callback != &umass_cam_cb)) {
2578                         umass_tr_error(xfer, error);
2579                 } else {
2580                         umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
2581                 }
2582                 return;
2583
2584         }
2585 }
2586
2587 static void
2588 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2589 {
2590         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2591             UMASS_T_CBI_DATA_WRITE, error);
2592 }
2593
2594 static void
2595 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
2596 {
2597         struct umass_softc *sc = usbd_xfer_softc(xfer);
2598         union ccb *ccb = sc->sc_transfer.ccb;
2599         struct usb_page_cache *pc;
2600         uint32_t residue;
2601         uint8_t status;
2602         int actlen;
2603
2604         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2605
2606         switch (USB_GET_STATE(xfer)) {
2607         case USB_ST_TRANSFERRED:
2608
2609                 if (actlen < sizeof(sc->sbl)) {
2610                         goto tr_setup;
2611                 }
2612                 pc = usbd_xfer_get_frame(xfer, 0);
2613                 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
2614
2615                 residue = (sc->sc_transfer.data_len -
2616                     sc->sc_transfer.actlen);
2617
2618                 /* dissect the information in the buffer */
2619
2620                 if (sc->sc_proto & UMASS_PROTO_UFI) {
2621
2622                         /*
2623                          * Section 3.4.3.1.3 specifies that the UFI command
2624                          * protocol returns an ASC and ASCQ in the interrupt
2625                          * data block.
2626                          */
2627
2628                         DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2629                             "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2630                             sc->sbl.ufi.ascq);
2631
2632                         status = (((sc->sbl.ufi.asc == 0) &&
2633                             (sc->sbl.ufi.ascq == 0)) ?
2634                             STATUS_CMD_OK : STATUS_CMD_FAILED);
2635
2636                         sc->sc_transfer.ccb = NULL;
2637
2638                         sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2639
2640                         (sc->sc_transfer.callback)
2641                             (sc, ccb, residue, status);
2642
2643                         return;
2644
2645                 } else {
2646
2647                         /* Command Interrupt Data Block */
2648
2649                         DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2650                             sc->sbl.common.type, sc->sbl.common.value);
2651
2652                         if (sc->sbl.common.type == IDB_TYPE_CCI) {
2653
2654                                 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2655
2656                                 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2657                                     (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2658                                     (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2659                                     STATUS_WIRE_FAILED);
2660
2661                                 sc->sc_transfer.ccb = NULL;
2662
2663                                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2664
2665                                 (sc->sc_transfer.callback)
2666                                     (sc, ccb, residue, status);
2667
2668                                 return;
2669                         }
2670                 }
2671
2672                 /* fallthrough */
2673
2674         case USB_ST_SETUP:
2675 tr_setup:
2676                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2677                 usbd_transfer_submit(xfer);
2678                 return;
2679
2680         default:                        /* Error */
2681                 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2682                     usbd_errstr(error));
2683                 umass_tr_error(xfer, error);
2684                 return;
2685
2686         }
2687 }
2688
2689 /*
2690  * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2691  */
2692
2693 static int
2694 umass_cam_attach_sim(struct umass_softc *sc)
2695 {
2696         struct cam_devq *devq;          /* Per device Queue */
2697
2698         /*
2699          * A HBA is attached to the CAM layer.
2700          *
2701          * The CAM layer will then after a while start probing for devices on
2702          * the bus. The number of SIMs is limited to one.
2703          */
2704
2705         devq = cam_simq_alloc(1 /* maximum openings */ );
2706         if (devq == NULL) {
2707                 return (ENOMEM);
2708         }
2709         sc->sc_sim = cam_sim_alloc
2710             (&umass_cam_action, &umass_cam_poll,
2711             DEVNAME_SIM,
2712             sc /* priv */ ,
2713             sc->sc_unit /* unit number */ ,
2714 #if (__FreeBSD_version >= 700037)
2715             &sc->sc_mtx /* mutex */ ,
2716 #endif
2717             1 /* maximum device openings */ ,
2718             0 /* maximum tagged device openings */ ,
2719             devq);
2720
2721         if (sc->sc_sim == NULL) {
2722                 cam_simq_free(devq);
2723                 return (ENOMEM);
2724         }
2725
2726 #if (__FreeBSD_version >= 700037)
2727         mtx_lock(&sc->sc_mtx);
2728 #endif
2729
2730 #if (__FreeBSD_version >= 700048)
2731         if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2732                 mtx_unlock(&sc->sc_mtx);
2733                 return (ENOMEM);
2734         }
2735 #else
2736         if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2737 #if (__FreeBSD_version >= 700037)
2738                 mtx_unlock(&sc->sc_mtx);
2739 #endif
2740                 return (ENOMEM);
2741         }
2742 #endif
2743
2744 #if (__FreeBSD_version >= 700037)
2745         mtx_unlock(&sc->sc_mtx);
2746 #endif
2747         return (0);
2748 }
2749
2750 static void
2751 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2752 {
2753 #if USB_DEBUG
2754         struct umass_softc *sc = NULL;
2755
2756         if (ccb->ccb_h.status != CAM_REQ_CMP) {
2757                 DPRINTF(sc, UDMASS_SCSI, "%s:%d Rescan failed, 0x%04x\n",
2758                     periph->periph_name, periph->unit_number,
2759                     ccb->ccb_h.status);
2760         } else {
2761                 DPRINTF(sc, UDMASS_SCSI, "%s%d: Rescan succeeded\n",
2762                     periph->periph_name, periph->unit_number);
2763         }
2764 #endif
2765
2766         xpt_free_path(ccb->ccb_h.path);
2767         free(ccb, M_USBDEV);
2768 }
2769
2770 static void
2771 umass_cam_rescan(struct umass_softc *sc)
2772 {
2773         struct cam_path *path;
2774         union ccb *ccb;
2775
2776         DPRINTF(sc, UDMASS_SCSI, "scbus%d: scanning for %d:%d:%d\n",
2777             cam_sim_path(sc->sc_sim),
2778             cam_sim_path(sc->sc_sim),
2779             sc->sc_unit, CAM_LUN_WILDCARD);
2780
2781         ccb = malloc(sizeof(*ccb), M_USBDEV, M_WAITOK | M_ZERO);
2782
2783         if (ccb == NULL) {
2784                 return;
2785         }
2786 #if (__FreeBSD_version >= 700037)
2787         mtx_lock(&sc->sc_mtx);
2788 #endif
2789
2790         if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sc_sim),
2791             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2792             != CAM_REQ_CMP) {
2793 #if (__FreeBSD_version >= 700037)
2794                 mtx_unlock(&sc->sc_mtx);
2795 #endif
2796                 free(ccb, M_USBDEV);
2797                 return;
2798         }
2799         xpt_setup_ccb(&ccb->ccb_h, path, 5 /* priority (low) */ );
2800         ccb->ccb_h.func_code = XPT_SCAN_BUS;
2801         ccb->ccb_h.cbfcnp = &umass_cam_rescan_callback;
2802         ccb->crcn.flags = CAM_FLAG_NONE;
2803         xpt_action(ccb);
2804
2805 #if (__FreeBSD_version >= 700037)
2806         mtx_unlock(&sc->sc_mtx);
2807 #endif
2808
2809         /* The scan is in progress now. */
2810 }
2811
2812 static void
2813 umass_cam_attach(struct umass_softc *sc)
2814 {
2815 #ifndef USB_DEBUG
2816         if (bootverbose)
2817 #endif
2818                 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2819                     sc->sc_name, cam_sim_path(sc->sc_sim),
2820                     sc->sc_unit, CAM_LUN_WILDCARD,
2821                     cam_sim_path(sc->sc_sim));
2822
2823         if (!cold) {
2824                 /*
2825                  * Notify CAM of the new device after a short delay. Any
2826                  * failure is benign, as the user can still do it by hand
2827                  * (camcontrol rescan <busno>). Only do this if we are not
2828                  * booting, because CAM does a scan after booting has
2829                  * completed, when interrupts have been enabled.
2830                  */
2831
2832                 /* scan the new sim */
2833                 umass_cam_rescan(sc);
2834         }
2835 }
2836
2837 /* umass_cam_detach
2838  *      detach from the CAM layer
2839  */
2840
2841 static void
2842 umass_cam_detach_sim(struct umass_softc *sc)
2843 {
2844         if (sc->sc_sim != NULL) {
2845                 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2846                         /* accessing the softc is not possible after this */
2847                         sc->sc_sim->softc = UMASS_GONE;
2848                         cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2849                 } else {
2850                         panic("%s: CAM layer is busy\n",
2851                             sc->sc_name);
2852                 }
2853                 sc->sc_sim = NULL;
2854         }
2855 }
2856
2857 /* umass_cam_action
2858  *      CAM requests for action come through here
2859  */
2860
2861 static void
2862 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2863 {
2864         struct umass_softc *sc = (struct umass_softc *)sim->softc;
2865
2866         if (sc == UMASS_GONE ||
2867             (sc != NULL && !usbd_device_attached(sc->sc_udev))) {
2868                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2869                 xpt_done(ccb);
2870                 return;
2871         }
2872         if (sc) {
2873 #if (__FreeBSD_version < 700037)
2874                 mtx_lock(&sc->sc_mtx);
2875 #endif
2876         }
2877         /*
2878          * Verify, depending on the operation to perform, that we either got
2879          * a valid sc, because an existing target was referenced, or
2880          * otherwise the SIM is addressed.
2881          *
2882          * This avoids bombing out at a printf and does give the CAM layer some
2883          * sensible feedback on errors.
2884          */
2885         switch (ccb->ccb_h.func_code) {
2886         case XPT_SCSI_IO:
2887         case XPT_RESET_DEV:
2888         case XPT_GET_TRAN_SETTINGS:
2889         case XPT_SET_TRAN_SETTINGS:
2890         case XPT_CALC_GEOMETRY:
2891                 /* the opcodes requiring a target. These should never occur. */
2892                 if (sc == NULL) {
2893                         DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2894                             "Invalid target (target needed)\n",
2895                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2896                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2897                             ccb->ccb_h.func_code);
2898
2899                         ccb->ccb_h.status = CAM_TID_INVALID;
2900                         xpt_done(ccb);
2901                         goto done;
2902                 }
2903                 break;
2904         case XPT_PATH_INQ:
2905         case XPT_NOOP:
2906                 /*
2907                  * The opcodes sometimes aimed at a target (sc is valid),
2908                  * sometimes aimed at the SIM (sc is invalid and target is
2909                  * CAM_TARGET_WILDCARD)
2910                  */
2911                 if ((sc == NULL) &&
2912                     (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2913                         DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2914                             "Invalid target (no wildcard)\n",
2915                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2916                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2917                             ccb->ccb_h.func_code);
2918
2919                         ccb->ccb_h.status = CAM_TID_INVALID;
2920                         xpt_done(ccb);
2921                         goto done;
2922                 }
2923                 break;
2924         default:
2925                 /* XXX Hm, we should check the input parameters */
2926                 break;
2927         }
2928
2929         /* Perform the requested action */
2930         switch (ccb->ccb_h.func_code) {
2931         case XPT_SCSI_IO:
2932                 {
2933                         uint8_t *cmd;
2934                         uint8_t dir;
2935
2936                         if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2937                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2938                         } else {
2939                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2940                         }
2941
2942                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2943                             "cmd: 0x%02x, flags: 0x%02x, "
2944                             "%db cmd/%db data/%db sense\n",
2945                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2946                             ccb->ccb_h.target_lun, cmd[0],
2947                             ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2948                             ccb->csio.dxfer_len, ccb->csio.sense_len);
2949
2950                         if (sc->sc_transfer.ccb) {
2951                                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2952                                     "I/O in progress, deferring\n",
2953                                     cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2954                                     ccb->ccb_h.target_lun);
2955                                 ccb->ccb_h.status = CAM_SCSI_BUSY;
2956                                 xpt_done(ccb);
2957                                 goto done;
2958                         }
2959                         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2960                         case CAM_DIR_IN:
2961                                 dir = DIR_IN;
2962                                 break;
2963                         case CAM_DIR_OUT:
2964                                 dir = DIR_OUT;
2965                                 DIF(UDMASS_SCSI,
2966                                     umass_dump_buffer(sc, ccb->csio.data_ptr,
2967                                     ccb->csio.dxfer_len, 48));
2968                                 break;
2969                         default:
2970                                 dir = DIR_NONE;
2971                         }
2972
2973                         ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2974
2975                         /*
2976                          * sc->sc_transform will convert the command to the
2977                          * command format needed by the specific command set
2978                          * and return the converted command in
2979                          * "sc->sc_transfer.cmd_data"
2980                          */
2981                         if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2982
2983                                 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2984
2985                                         /*
2986                                          * Umass devices don't generally report their serial numbers
2987                                          * in the usual SCSI way.  Emulate it here.
2988                                          */
2989                                         if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2990                                             sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER &&
2991                                             sc->sc_udev != NULL &&
2992                                             sc->sc_udev->serial != NULL &&
2993                                             sc->sc_udev->serial[0] != '\0') {
2994                                                 struct scsi_vpd_unit_serial_number *vpd_serial;
2995
2996                                                 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2997                                                 vpd_serial->length = strlen(sc->sc_udev->serial);
2998                                                 if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2999                                                         vpd_serial->length = sizeof(vpd_serial->serial_num);
3000                                                 memcpy(vpd_serial->serial_num, sc->sc_udev->serial, vpd_serial->length);
3001                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3002                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3003                                                 xpt_done(ccb);
3004                                                 goto done;
3005                                         }
3006
3007                                         /*
3008                                          * Handle EVPD inquiry for broken devices first
3009                                          * NO_INQUIRY also implies NO_INQUIRY_EVPD
3010                                          */
3011                                         if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
3012                                             (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
3013                                                 struct scsi_sense_data *sense;
3014
3015                                                 sense = &ccb->csio.sense_data;
3016                                                 bzero(sense, sizeof(*sense));
3017                                                 sense->error_code = SSD_CURRENT_ERROR;
3018                                                 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
3019                                                 sense->add_sense_code = 0x24;
3020                                                 sense->extra_len = 10;
3021                                                 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3022                                                 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
3023                                                     CAM_AUTOSNS_VALID;
3024                                                 xpt_done(ccb);
3025                                                 goto done;
3026                                         }
3027                                         /*
3028                                          * Return fake inquiry data for
3029                                          * broken devices
3030                                          */
3031                                         if (sc->sc_quirks & NO_INQUIRY) {
3032                                                 memcpy(ccb->csio.data_ptr, &fake_inq_data,
3033                                                     sizeof(fake_inq_data));
3034                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3035                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3036                                                 xpt_done(ccb);
3037                                                 goto done;
3038                                         }
3039                                         if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3040                                                 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
3041                                         }
3042                                 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
3043                                         if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
3044                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3045                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3046                                                 xpt_done(ccb);
3047                                                 goto done;
3048                                         }
3049                                 }
3050                                 umass_command_start(sc, dir, ccb->csio.data_ptr,
3051                                     ccb->csio.dxfer_len,
3052                                     ccb->ccb_h.timeout,
3053                                     &umass_cam_cb, ccb);
3054                         }
3055                         break;
3056                 }
3057         case XPT_PATH_INQ:
3058                 {
3059                         struct ccb_pathinq *cpi = &ccb->cpi;
3060
3061                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
3062                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3063                             ccb->ccb_h.target_lun);
3064
3065                         /* host specific information */
3066                         cpi->version_num = 1;
3067                         cpi->hba_inquiry = 0;
3068                         cpi->target_sprt = 0;
3069                         cpi->hba_misc = PIM_NO_6_BYTE;
3070                         cpi->hba_eng_cnt = 0;
3071                         cpi->max_target = UMASS_SCSIID_MAX;     /* one target */
3072                         cpi->initiator_id = UMASS_SCSIID_HOST;
3073                         strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3074                         strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
3075                         strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3076                         cpi->unit_number = cam_sim_unit(sim);
3077                         cpi->bus_id = sc->sc_unit;
3078 #if (__FreeBSD_version >= 700025)
3079                         cpi->protocol = PROTO_SCSI;
3080                         cpi->protocol_version = SCSI_REV_2;
3081                         cpi->transport = XPORT_USB;
3082                         cpi->transport_version = 0;
3083 #endif
3084                         if (sc == NULL) {
3085                                 cpi->base_transfer_speed = 0;
3086                                 cpi->max_lun = 0;
3087                         } else {
3088                                 if (sc->sc_quirks & FLOPPY_SPEED) {
3089                                         cpi->base_transfer_speed =
3090                                             UMASS_FLOPPY_TRANSFER_SPEED;
3091                                 } else if (usbd_get_speed(sc->sc_udev) ==
3092                                     USB_SPEED_HIGH) {
3093                                         cpi->base_transfer_speed =
3094                                             UMASS_HIGH_TRANSFER_SPEED;
3095                                 } else {
3096                                         cpi->base_transfer_speed =
3097                                             UMASS_FULL_TRANSFER_SPEED;
3098                                 }
3099                                 cpi->max_lun = sc->sc_maxlun;
3100                         }
3101
3102                         cpi->ccb_h.status = CAM_REQ_CMP;
3103                         xpt_done(ccb);
3104                         break;
3105                 }
3106         case XPT_RESET_DEV:
3107                 {
3108                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
3109                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3110                             ccb->ccb_h.target_lun);
3111
3112                         umass_reset(sc);
3113
3114                         ccb->ccb_h.status = CAM_REQ_CMP;
3115                         xpt_done(ccb);
3116                         break;
3117                 }
3118         case XPT_GET_TRAN_SETTINGS:
3119                 {
3120                         struct ccb_trans_settings *cts = &ccb->cts;
3121
3122                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
3123                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3124                             ccb->ccb_h.target_lun);
3125
3126 #if (__FreeBSD_version >= 700025)
3127                         cts->protocol = PROTO_SCSI;
3128                         cts->protocol_version = SCSI_REV_2;
3129                         cts->transport = XPORT_USB;
3130                         cts->transport_version = 0;
3131                         cts->xport_specific.valid = 0;
3132 #else
3133                         cts->valid = 0;
3134                         cts->flags = 0; /* no disconnection, tagging */
3135 #endif
3136                         ccb->ccb_h.status = CAM_REQ_CMP;
3137                         xpt_done(ccb);
3138                         break;
3139                 }
3140         case XPT_SET_TRAN_SETTINGS:
3141                 {
3142                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3143                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3144                             ccb->ccb_h.target_lun);
3145
3146                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3147                         xpt_done(ccb);
3148                         break;
3149                 }
3150         case XPT_CALC_GEOMETRY:
3151                 {
3152                         cam_calc_geometry(&ccb->ccg, /* extended */ 1);
3153                         xpt_done(ccb);
3154                         break;
3155                 }
3156         case XPT_NOOP:
3157                 {
3158                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
3159                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3160                             ccb->ccb_h.target_lun);
3161
3162                         ccb->ccb_h.status = CAM_REQ_CMP;
3163                         xpt_done(ccb);
3164                         break;
3165                 }
3166         default:
3167                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
3168                     "Not implemented\n",
3169                     sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3170                     ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
3171
3172                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3173                 xpt_done(ccb);
3174                 break;
3175         }
3176
3177 done:
3178 #if (__FreeBSD_version < 700037)
3179         if (sc) {
3180                 mtx_unlock(&sc->sc_mtx);
3181         }
3182 #endif
3183         return;
3184 }
3185
3186 static void
3187 umass_cam_poll(struct cam_sim *sim)
3188 {
3189         struct umass_softc *sc = (struct umass_softc *)sim->softc;
3190
3191         if (sc == UMASS_GONE)
3192                 return;
3193
3194         DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
3195
3196         usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
3197 }
3198
3199
3200 /* umass_cam_cb
3201  *      finalise a completed CAM command
3202  */
3203
3204 static void
3205 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3206     uint8_t status)
3207 {
3208         ccb->csio.resid = residue;
3209
3210         switch (status) {
3211         case STATUS_CMD_OK:
3212                 ccb->ccb_h.status = CAM_REQ_CMP;
3213                 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
3214                     (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3215                     (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3216                         struct scsi_read_capacity_data *rcap;
3217                         uint32_t maxsector;
3218
3219                         rcap = (void *)(ccb->csio.data_ptr);
3220                         maxsector = scsi_4btoul(rcap->addr) - 1;
3221                         scsi_ulto4b(maxsector, rcap->addr);
3222                 }
3223                 /*
3224                  * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
3225                  * of pages supported by the device - otherwise, CAM
3226                  * will never ask us for the serial number if the
3227                  * device cannot handle that by itself.
3228                  */
3229                 if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
3230                     sc->sc_transfer.cmd_data[0] == INQUIRY &&
3231                     (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
3232                     sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
3233                     sc->sc_udev != NULL &&
3234                     sc->sc_udev->serial != NULL &&
3235                     sc->sc_udev->serial[0] != '\0') {
3236                         struct ccb_scsiio *csio;
3237                         struct scsi_vpd_supported_page_list *page_list;
3238
3239                         csio = &ccb->csio;
3240                         page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
3241                         if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
3242                                 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
3243                                 page_list->length++;
3244                         }
3245                 }
3246                 xpt_done(ccb);
3247                 break;
3248
3249         case STATUS_CMD_UNKNOWN:
3250         case STATUS_CMD_FAILED:
3251
3252                 /* fetch sense data */
3253
3254                 /* the rest of the command was filled in at attach */
3255                 sc->cam_scsi_sense.length = ccb->csio.sense_len;
3256
3257                 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
3258                     "sense data\n", ccb->csio.sense_len);
3259
3260                 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
3261                     sizeof(sc->cam_scsi_sense))) {
3262
3263                         if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
3264                             (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
3265                                 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
3266                         }
3267                         umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
3268                             ccb->csio.sense_len, ccb->ccb_h.timeout,
3269                             &umass_cam_sense_cb, ccb);
3270                 }
3271                 break;
3272
3273         default:
3274                 /*
3275                  * the wire protocol failed and will have recovered
3276                  * (hopefully).  We return an error to CAM and let CAM retry
3277                  * the command if necessary.
3278                  */
3279                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3280                 xpt_done(ccb);
3281                 break;
3282         }
3283 }
3284
3285 /*
3286  * Finalise a completed autosense operation
3287  */
3288 static void
3289 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3290     uint8_t status)
3291 {
3292         uint8_t *cmd;
3293         uint8_t key;
3294
3295         switch (status) {
3296         case STATUS_CMD_OK:
3297         case STATUS_CMD_UNKNOWN:
3298         case STATUS_CMD_FAILED:
3299
3300                 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
3301                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
3302                 } else {
3303                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
3304                 }
3305
3306                 key = (ccb->csio.sense_data.flags & SSD_KEY);
3307
3308                 /*
3309                  * Getting sense data always succeeds (apart from wire
3310                  * failures):
3311                  */
3312                 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3313                     (cmd[0] == INQUIRY) &&
3314                     (key == SSD_KEY_UNIT_ATTENTION)) {
3315                         /*
3316                          * Ignore unit attention errors in the case where
3317                          * the Unit Attention state is not cleared on
3318                          * REQUEST SENSE. They will appear again at the next
3319                          * command.
3320                          */
3321                         ccb->ccb_h.status = CAM_REQ_CMP;
3322                 } else if (key == SSD_KEY_NO_SENSE) {
3323                         /*
3324                          * No problem after all (in the case of CBI without
3325                          * CCI)
3326                          */
3327                         ccb->ccb_h.status = CAM_REQ_CMP;
3328                 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3329                             (cmd[0] == READ_CAPACITY) &&
3330                     (key == SSD_KEY_UNIT_ATTENTION)) {
3331                         /*
3332                          * Some devices do not clear the unit attention error
3333                          * on request sense. We insert a test unit ready
3334                          * command to make sure we clear the unit attention
3335                          * condition, then allow the retry to proceed as
3336                          * usual.
3337                          */
3338
3339                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3340                             | CAM_AUTOSNS_VALID;
3341                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3342
3343 #if 0
3344                         DELAY(300000);
3345 #endif
3346                         DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
3347                             "TEST_UNIT_READY\n");
3348
3349                         /* the rest of the command was filled in at attach */
3350
3351                         if (umass_std_transform(sc, ccb,
3352                             &sc->cam_scsi_test_unit_ready.opcode,
3353                             sizeof(sc->cam_scsi_test_unit_ready))) {
3354                                 umass_command_start(sc, DIR_NONE, NULL, 0,
3355                                     ccb->ccb_h.timeout,
3356                                     &umass_cam_quirk_cb, ccb);
3357                         }
3358                         break;
3359                 } else {
3360                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3361                             | CAM_AUTOSNS_VALID;
3362                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3363                 }
3364                 xpt_done(ccb);
3365                 break;
3366
3367         default:
3368                 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
3369                     "status %d\n", status);
3370                 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3371                 xpt_done(ccb);
3372         }
3373 }
3374
3375 /*
3376  * This completion code just handles the fact that we sent a test-unit-ready
3377  * after having previously failed a READ CAPACITY with CHECK_COND.  Even
3378  * though this command succeeded, we have to tell CAM to retry.
3379  */
3380 static void
3381 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3382     uint8_t status)
3383 {
3384         DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
3385             "returned status %d\n", status);
3386
3387         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3388             | CAM_AUTOSNS_VALID;
3389         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3390         xpt_done(ccb);
3391 }
3392
3393 /*
3394  * SCSI specific functions
3395  */
3396
3397 static uint8_t
3398 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3399     uint8_t cmd_len)
3400 {
3401         if ((cmd_len == 0) ||
3402             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3403                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3404                     "length: %d bytes\n", cmd_len);
3405                 return (0);             /* failure */
3406         }
3407         sc->sc_transfer.cmd_len = cmd_len;
3408
3409         switch (cmd_ptr[0]) {
3410         case TEST_UNIT_READY:
3411                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3412                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3413                             "to START_UNIT\n");
3414                         bzero(sc->sc_transfer.cmd_data, cmd_len);
3415                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3416                         sc->sc_transfer.cmd_data[4] = SSS_START;
3417                         return (1);
3418                 }
3419                 break;
3420
3421         case INQUIRY:
3422                 /*
3423                  * some drives wedge when asked for full inquiry
3424                  * information.
3425                  */
3426                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3427                         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3428                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3429                         return (1);
3430                 }
3431                 break;
3432         }
3433
3434         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3435         return (1);
3436 }
3437
3438 static uint8_t
3439 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
3440 {
3441         if ((cmd_len == 0) ||
3442             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3443                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3444                     "length: %d bytes\n", cmd_len);
3445                 return (0);             /* failure */
3446         }
3447         switch (cmd_ptr[0]) {
3448                 /* these commands are defined in RBC: */
3449         case READ_10:
3450         case READ_CAPACITY:
3451         case START_STOP_UNIT:
3452         case SYNCHRONIZE_CACHE:
3453         case WRITE_10:
3454         case 0x2f:                      /* VERIFY_10 is absent from
3455                                          * scsi_all.h??? */
3456         case INQUIRY:
3457         case MODE_SELECT_10:
3458         case MODE_SENSE_10:
3459         case TEST_UNIT_READY:
3460         case WRITE_BUFFER:
3461                 /*
3462                  * The following commands are not listed in my copy of the
3463                  * RBC specs. CAM however seems to want those, and at least
3464                  * the Sony DSC device appears to support those as well
3465                  */
3466         case REQUEST_SENSE:
3467         case PREVENT_ALLOW:
3468
3469                 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3470
3471                 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
3472                         bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len);
3473                         cmd_len = 12;
3474                 }
3475                 sc->sc_transfer.cmd_len = cmd_len;
3476                 return (1);             /* sucess */
3477
3478                 /* All other commands are not legal in RBC */
3479         default:
3480                 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
3481                     "command 0x%02x\n", cmd_ptr[0]);
3482                 return (0);             /* failure */
3483         }
3484 }
3485
3486 static uint8_t
3487 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3488     uint8_t cmd_len)
3489 {
3490         if ((cmd_len == 0) ||
3491             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3492                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3493                     "length: %d bytes\n", cmd_len);
3494                 return (0);             /* failure */
3495         }
3496         /* An UFI command is always 12 bytes in length */
3497         sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
3498
3499         /* Zero the command data */
3500         bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH);
3501
3502         switch (cmd_ptr[0]) {
3503                 /*
3504                  * Commands of which the format has been verified. They
3505                  * should work. Copy the command into the (zeroed out)
3506                  * destination buffer.
3507                  */
3508         case TEST_UNIT_READY:
3509                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3510                         /*
3511                          * Some devices do not support this command. Start
3512                          * Stop Unit should give the same results
3513                          */
3514                         DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
3515                             "to START_UNIT\n");
3516
3517                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3518                         sc->sc_transfer.cmd_data[4] = SSS_START;
3519                         return (1);
3520                 }
3521                 break;
3522
3523         case REZERO_UNIT:
3524         case REQUEST_SENSE:
3525         case FORMAT_UNIT:
3526         case INQUIRY:
3527         case START_STOP_UNIT:
3528         case SEND_DIAGNOSTIC:
3529         case PREVENT_ALLOW:
3530         case READ_CAPACITY:
3531         case READ_10:
3532         case WRITE_10:
3533         case POSITION_TO_ELEMENT:       /* SEEK_10 */
3534         case WRITE_AND_VERIFY:
3535         case VERIFY:
3536         case MODE_SELECT_10:
3537         case MODE_SENSE_10:
3538         case READ_12:
3539         case WRITE_12:
3540         case READ_FORMAT_CAPACITIES:
3541                 break;
3542
3543                 /*
3544                  * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3545                  * required for UFI devices, so it is appropriate to fake
3546                  * success.
3547                  */
3548         case SYNCHRONIZE_CACHE:
3549                 return (2);
3550
3551         default:
3552                 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
3553                     "command 0x%02x\n", cmd_ptr[0]);
3554                 return (0);             /* failure */
3555         }
3556
3557         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3558         return (1);                     /* success */
3559 }
3560
3561 /*
3562  * 8070i (ATAPI) specific functions
3563  */
3564 static uint8_t
3565 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3566     uint8_t cmd_len)
3567 {
3568         if ((cmd_len == 0) ||
3569             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3570                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3571                     "length: %d bytes\n", cmd_len);
3572                 return (0);             /* failure */
3573         }
3574         /* An ATAPI command is always 12 bytes in length. */
3575         sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
3576
3577         /* Zero the command data */
3578         bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH);
3579
3580         switch (cmd_ptr[0]) {
3581                 /*
3582                  * Commands of which the format has been verified. They
3583                  * should work. Copy the command into the destination
3584                  * buffer.
3585                  */
3586         case INQUIRY:
3587                 /*
3588                  * some drives wedge when asked for full inquiry
3589                  * information.
3590                  */
3591                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3592                         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3593
3594                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3595                         return (1);
3596                 }
3597                 break;
3598
3599         case TEST_UNIT_READY:
3600                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3601                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3602                             "to START_UNIT\n");
3603                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3604                         sc->sc_transfer.cmd_data[4] = SSS_START;
3605                         return (1);
3606                 }
3607                 break;
3608
3609         case REZERO_UNIT:
3610         case REQUEST_SENSE:
3611         case START_STOP_UNIT:
3612         case SEND_DIAGNOSTIC:
3613         case PREVENT_ALLOW:
3614         case READ_CAPACITY:
3615         case READ_10:
3616         case WRITE_10:
3617         case POSITION_TO_ELEMENT:       /* SEEK_10 */
3618         case SYNCHRONIZE_CACHE:
3619         case MODE_SELECT_10:
3620         case MODE_SENSE_10:
3621         case READ_BUFFER:
3622         case 0x42:                      /* READ_SUBCHANNEL */
3623         case 0x43:                      /* READ_TOC */
3624         case 0x44:                      /* READ_HEADER */
3625         case 0x47:                      /* PLAY_MSF (Play Minute/Second/Frame) */
3626         case 0x48:                      /* PLAY_TRACK */
3627         case 0x49:                      /* PLAY_TRACK_REL */
3628         case 0x4b:                      /* PAUSE */
3629         case 0x51:                      /* READ_DISK_INFO */
3630         case 0x52:                      /* READ_TRACK_INFO */
3631         case 0x54:                      /* SEND_OPC */
3632         case 0x59:                      /* READ_MASTER_CUE */
3633         case 0x5b:                      /* CLOSE_TR_SESSION */
3634         case 0x5c:                      /* READ_BUFFER_CAP */
3635         case 0x5d:                      /* SEND_CUE_SHEET */
3636         case 0xa1:                      /* BLANK */
3637         case 0xa5:                      /* PLAY_12 */
3638         case 0xa6:                      /* EXCHANGE_MEDIUM */
3639         case 0xad:                      /* READ_DVD_STRUCTURE */
3640         case 0xbb:                      /* SET_CD_SPEED */
3641         case 0xe5:                      /* READ_TRACK_INFO_PHILIPS */
3642                 break;;
3643
3644         case READ_12:
3645         case WRITE_12:
3646         default:
3647                 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
3648                     "command 0x%02x - trying anyway\n",
3649                     cmd_ptr[0]);
3650                 break;;
3651         }
3652
3653         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3654         return (1);                     /* success */
3655 }
3656
3657 static uint8_t
3658 umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3659     uint8_t cmdlen)
3660 {
3661         return (0);                     /* failure */
3662 }
3663
3664 static uint8_t
3665 umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3666     uint8_t *cmd, uint8_t cmdlen)
3667 {
3668         uint8_t retval;
3669
3670         retval = (sc->sc_transform) (sc, cmd, cmdlen);
3671
3672         if (retval == 2) {
3673                 ccb->ccb_h.status = CAM_REQ_CMP;
3674                 xpt_done(ccb);
3675                 return (0);
3676         } else if (retval == 0) {
3677                 ccb->ccb_h.status = CAM_REQ_INVALID;
3678                 xpt_done(ccb);
3679                 return (0);
3680         }
3681         /* Command should be executed */
3682         return (1);
3683 }
3684
3685 #if USB_DEBUG
3686 static void
3687 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3688 {
3689         uint8_t *c = cbw->CBWCDB;
3690
3691         uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3692         uint32_t tag = UGETDW(cbw->dCBWTag);
3693
3694         uint8_t clen = cbw->bCDBLength;
3695         uint8_t flags = cbw->bCBWFlags;
3696         uint8_t lun = cbw->bCBWLUN;
3697
3698         DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3699             "(0x%02x%02x%02x%02x%02x%02x%s), "
3700             "data = %db, lun = %d, dir = %s\n",
3701             tag, clen,
3702             c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3703             dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3704             (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3705 }
3706
3707 static void
3708 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3709 {
3710         uint32_t sig = UGETDW(csw->dCSWSignature);
3711         uint32_t tag = UGETDW(csw->dCSWTag);
3712         uint32_t res = UGETDW(csw->dCSWDataResidue);
3713         uint8_t status = csw->bCSWStatus;
3714
3715         DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3716             "res = %d, status = 0x%02x (%s)\n",
3717             tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3718             tag, res,
3719             status, (status == CSWSTATUS_GOOD ? "good" :
3720             (status == CSWSTATUS_FAILED ? "failed" :
3721             (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3722 }
3723
3724 static void
3725 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3726 {
3727         uint8_t *c = cmd;
3728         uint8_t dir = sc->sc_transfer.dir;
3729
3730         DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3731             "(0x%02x%02x%02x%02x%02x%02x%s), "
3732             "data = %db, dir = %s\n",
3733             cmdlen,
3734             c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3735             sc->sc_transfer.data_len,
3736             (dir == DIR_IN ? "in" :
3737             (dir == DIR_OUT ? "out" :
3738             (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3739 }
3740
3741 static void
3742 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3743     uint32_t printlen)
3744 {
3745         uint32_t i, j;
3746         char s1[40];
3747         char s2[40];
3748         char s3[5];
3749
3750         s1[0] = '\0';
3751         s3[0] = '\0';
3752
3753         sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3754         for (i = 0; (i < buflen) && (i < printlen); i++) {
3755                 j = i % 16;
3756                 if (j == 0 && i != 0) {
3757                         DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3758                             s1, s2);
3759                         s2[0] = '\0';
3760                 }
3761                 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3762         }
3763         if (buflen > printlen)
3764                 sprintf(s3, " ...");
3765         DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3766             s1, s2, s3);
3767 }
3768
3769 #endif