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