]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/storage/umass.c
MFC r199062
[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_DEFAULT,
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_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
754                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
755                 NO_INQUIRY
756         },
757         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
758                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
759                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
760         },
761         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
762                 UMASS_PROTO_SCSI,
763                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
764         },
765         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
766                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
767                 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
768         },
769         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
770                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
771                 IGNORE_RESIDUE
772         },
773         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
774                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
775                 IGNORE_RESIDUE
776         },
777         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
778                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
779                 IGNORE_RESIDUE
780         },
781         {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
782                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
783                 READ_CAPACITY_OFFBY1
784         },
785         {USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
786                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
787                 NO_INQUIRY
788         },
789         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
790                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
791                 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
792         },
793         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
794                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
795                 NO_QUIRKS
796         },
797         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
798                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
799                 NO_QUIRKS
800         },
801         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
802                 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
803                 NO_QUIRKS
804         },
805         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
806                 UMASS_PROTO_SCSI,
807                 NO_QUIRKS
808         },
809         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
810                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
811                 NO_QUIRKS
812         },
813         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
814                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
815                 NO_GETMAXLUN
816         },
817         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
818                 UMASS_PROTO_SCSI,
819                 NO_GETMAXLUN
820         },
821         {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
822                 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
823                 NO_GETMAXLUN
824         },
825         {USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
826                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
827                 SHUTTLE_INIT
828         },
829         {USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
830                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
831                 IGNORE_RESIDUE
832         },
833         {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
834                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
835                 NO_INQUIRY
836         },
837         {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
838                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
839                 NO_INQUIRY
840         },
841         {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, 0x0500,
842                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
843                 RBC_PAD_TO_12
844         },
845         {USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
846                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
847                 NO_INQUIRY
848         },
849         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
850                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
851                 RBC_PAD_TO_12
852         },
853         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
854                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
855                 RBC_PAD_TO_12
856         },
857         {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
858                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
859                 NO_QUIRKS
860         },
861         {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
862                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
863                 NO_QUIRKS
864         },
865         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
866                 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
867                 NO_QUIRKS
868         },
869         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
870                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
871                 NO_GETMAXLUN
872         },
873         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
874                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
875                 NO_GETMAXLUN
876         },
877         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
878                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
879                 NO_INQUIRY
880         },
881         {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
882                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
883                 NO_GETMAXLUN
884         },
885         {USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
886                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
887                 NO_QUIRKS
888         },
889         {USB_VENDOR_SUPERTOP, USB_PRODUCT_SUPERTOP_IDE, RID_WILDCARD,
890                 UMASS_PROTO_DEFAULT,
891                 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
892         },
893         {USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
894                 UMASS_PROTO_SCSI,
895                 NO_QUIRKS
896         },
897         {USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
898                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
899                 NO_QUIRKS
900         },
901         {USB_VENDOR_TECLAST, USB_PRODUCT_TECLAST_TLC300, RID_WILDCARD,
902                 UMASS_PROTO_DEFAULT,
903                 NO_TEST_UNIT_READY | NO_SYNCHRONIZE_CACHE
904         },
905         {USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
906                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
907                 NO_INQUIRY
908         },
909         {USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
910                 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
911                 IGNORE_RESIDUE
912         },
913         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
914                 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
915                 NO_QUIRKS
916         },
917         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
918                 UMASS_PROTO_RBC,
919                 NO_QUIRKS
920         },
921         {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
922                 UMASS_PROTO_SCSI,
923                 NO_QUIRKS
924         },
925         {USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
926                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
927                 NO_QUIRKS
928         },
929         {USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
930                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
931                 NO_SYNCHRONIZE_CACHE
932         },
933         {USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
934                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
935                 NO_INQUIRY
936         },
937         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
938                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
939                 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
940         },
941         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, 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_MYBOOK, RID_WILDCARD,
946                 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
947                 NO_INQUIRY_EVPD
948         },
949         {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYPASSWORD, RID_WILDCARD,
950                 UMASS_PROTO_DEFAULT,
951                 FORCE_SHORT_INQUIRY
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         mtx_destroy(&sc->sc_mtx);
1673
1674         return (0);                     /* success */
1675 }
1676
1677 static void
1678 umass_init_shuttle(struct umass_softc *sc)
1679 {
1680         struct usb_device_request req;
1681         usb_error_t err;
1682         uint8_t status[2] = {0, 0};
1683
1684         /*
1685          * The Linux driver does this, but no one can tell us what the
1686          * command does.
1687          */
1688         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1689         req.bRequest = 1;               /* XXX unknown command */
1690         USETW(req.wValue, 0);
1691         req.wIndex[0] = sc->sc_iface_no;
1692         req.wIndex[1] = 0;
1693         USETW(req.wLength, sizeof(status));
1694         err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1695
1696         DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1697             status[0], status[1]);
1698 }
1699
1700 /*
1701  * Generic functions to handle transfers
1702  */
1703
1704 static void
1705 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1706 {
1707         DPRINTF(sc, UDMASS_GEN, "transfer index = "
1708             "%d\n", xfer_index);
1709
1710         if (sc->sc_xfer[xfer_index]) {
1711                 sc->sc_last_xfer_index = xfer_index;
1712                 usbd_transfer_start(sc->sc_xfer[xfer_index]);
1713         } else {
1714                 umass_cancel_ccb(sc);
1715         }
1716 }
1717
1718 static void
1719 umass_reset(struct umass_softc *sc)
1720 {
1721         DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1722
1723         /*
1724          * stop the last transfer, if not already stopped:
1725          */
1726         usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1727         umass_transfer_start(sc, 0);
1728 }
1729
1730 static void
1731 umass_cancel_ccb(struct umass_softc *sc)
1732 {
1733         union ccb *ccb;
1734
1735         mtx_assert(&sc->sc_mtx, MA_OWNED);
1736
1737         ccb = sc->sc_transfer.ccb;
1738         sc->sc_transfer.ccb = NULL;
1739         sc->sc_last_xfer_index = 0;
1740
1741         if (ccb) {
1742                 (sc->sc_transfer.callback)
1743                     (sc, ccb, (sc->sc_transfer.data_len -
1744                     sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1745         }
1746 }
1747
1748 static void
1749 umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1750 {
1751         struct umass_softc *sc = usbd_xfer_softc(xfer);
1752
1753         if (error != USB_ERR_CANCELLED) {
1754
1755                 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1756                     "reset\n", usbd_errstr(error));
1757         }
1758         umass_cancel_ccb(sc);
1759 }
1760
1761 /*
1762  * BBB protocol specific functions
1763  */
1764
1765 static void
1766 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1767 {
1768         struct umass_softc *sc = usbd_xfer_softc(xfer);
1769         struct usb_device_request req;
1770         struct usb_page_cache *pc;
1771
1772         switch (USB_GET_STATE(xfer)) {
1773         case USB_ST_TRANSFERRED:
1774                 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1775                 return;
1776
1777         case USB_ST_SETUP:
1778                 /*
1779                  * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1780                  *
1781                  * For Reset Recovery the host shall issue in the following order:
1782                  * a) a Bulk-Only Mass Storage Reset
1783                  * b) a Clear Feature HALT to the Bulk-In endpoint
1784                  * c) a Clear Feature HALT to the Bulk-Out endpoint
1785                  *
1786                  * This is done in 3 steps, using 3 transfers:
1787                  * UMASS_T_BBB_RESET1
1788                  * UMASS_T_BBB_RESET2
1789                  * UMASS_T_BBB_RESET3
1790                  */
1791
1792                 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1793
1794                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1795                 req.bRequest = UR_BBB_RESET;    /* bulk only reset */
1796                 USETW(req.wValue, 0);
1797                 req.wIndex[0] = sc->sc_iface_no;
1798                 req.wIndex[1] = 0;
1799                 USETW(req.wLength, 0);
1800
1801                 pc = usbd_xfer_get_frame(xfer, 0);
1802                 usbd_copy_in(pc, 0, &req, sizeof(req));
1803
1804                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1805                 usbd_xfer_set_frames(xfer, 1);
1806                 usbd_transfer_submit(xfer);
1807                 return;
1808
1809         default:                        /* Error */
1810                 umass_tr_error(xfer, error);
1811                 return;
1812
1813         }
1814 }
1815
1816 static void
1817 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1818 {
1819         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1820             UMASS_T_BBB_DATA_READ, error);
1821 }
1822
1823 static void
1824 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1825 {
1826         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1827             UMASS_T_BBB_DATA_WRITE, error);
1828 }
1829
1830 static void
1831 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1832     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1833 {
1834         struct umass_softc *sc = usbd_xfer_softc(xfer);
1835
1836         switch (USB_GET_STATE(xfer)) {
1837         case USB_ST_TRANSFERRED:
1838 tr_transferred:
1839                 umass_transfer_start(sc, next_xfer);
1840                 return;
1841
1842         case USB_ST_SETUP:
1843                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1844                         goto tr_transferred;
1845                 }
1846                 return;
1847
1848         default:                        /* Error */
1849                 umass_tr_error(xfer, error);
1850                 return;
1851
1852         }
1853 }
1854
1855 static void
1856 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1857 {
1858         struct umass_softc *sc = usbd_xfer_softc(xfer);
1859         union ccb *ccb = sc->sc_transfer.ccb;
1860         struct usb_page_cache *pc;
1861         uint32_t tag;
1862
1863         switch (USB_GET_STATE(xfer)) {
1864         case USB_ST_TRANSFERRED:
1865                 umass_transfer_start
1866                     (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1867                     (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1868                     UMASS_T_BBB_STATUS));
1869                 return;
1870
1871         case USB_ST_SETUP:
1872
1873                 sc->sc_status_try = 0;
1874
1875                 if (ccb) {
1876
1877                         /*
1878                          * the initial value is not important,
1879                          * as long as the values are unique:
1880                          */
1881                         tag = UGETDW(sc->cbw.dCBWTag) + 1;
1882
1883                         USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1884                         USETDW(sc->cbw.dCBWTag, tag);
1885
1886                         /*
1887                          * dCBWDataTransferLength:
1888                          *   This field indicates the number of bytes of data that the host
1889                          *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1890                          *   the Direction bit) during the execution of this command. If this
1891                          *   field is set to 0, the device will expect that no data will be
1892                          *   transferred IN or OUT during this command, regardless of the value
1893                          *   of the Direction bit defined in dCBWFlags.
1894                          */
1895                         USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1896
1897                         /*
1898                          * dCBWFlags:
1899                          *   The bits of the Flags field are defined as follows:
1900                          *     Bits 0-6  reserved
1901                          *     Bit  7    Direction - this bit shall be ignored if the
1902                          *                           dCBWDataTransferLength field is zero.
1903                          *               0 = data Out from host to device
1904                          *               1 = data In from device to host
1905                          */
1906                         sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1907                             CBWFLAGS_IN : CBWFLAGS_OUT);
1908                         sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1909
1910                         if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1911                                 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1912                                 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1913                         }
1914                         sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1915
1916                         bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB,
1917                             sc->sc_transfer.cmd_len);
1918
1919                         bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len,
1920                             sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len);
1921
1922                         DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1923
1924                         pc = usbd_xfer_get_frame(xfer, 0);
1925                         usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1926                         usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1927
1928                         usbd_transfer_submit(xfer);
1929                 }
1930                 return;
1931
1932         default:                        /* Error */
1933                 umass_tr_error(xfer, error);
1934                 return;
1935
1936         }
1937 }
1938
1939 static void
1940 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1941 {
1942         struct umass_softc *sc = usbd_xfer_softc(xfer);
1943         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1944 #ifndef UMASS_EXT_BUFFER
1945         struct usb_page_cache *pc;
1946 #endif
1947         int actlen, sumlen;
1948
1949         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1950
1951         switch (USB_GET_STATE(xfer)) {
1952         case USB_ST_TRANSFERRED:
1953 #ifndef UMASS_EXT_BUFFER
1954                 pc = usbd_xfer_get_frame(xfer, 0);
1955                 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1956 #endif
1957                 sc->sc_transfer.data_rem -= actlen;
1958                 sc->sc_transfer.data_ptr += actlen;
1959                 sc->sc_transfer.actlen += actlen;
1960
1961                 if (actlen < sumlen) {
1962                         /* short transfer */
1963                         sc->sc_transfer.data_rem = 0;
1964                 }
1965         case USB_ST_SETUP:
1966                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1967                     max_bulk, sc->sc_transfer.data_rem);
1968
1969                 if (sc->sc_transfer.data_rem == 0) {
1970                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1971                         return;
1972                 }
1973                 if (max_bulk > sc->sc_transfer.data_rem) {
1974                         max_bulk = sc->sc_transfer.data_rem;
1975                 }
1976                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1977
1978 #ifdef UMASS_EXT_BUFFER
1979                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1980                     max_bulk);
1981 #else
1982                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1983 #endif
1984                 usbd_transfer_submit(xfer);
1985                 return;
1986
1987         default:                        /* Error */
1988                 if (error == USB_ERR_CANCELLED) {
1989                         umass_tr_error(xfer, error);
1990                 } else {
1991                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1992                 }
1993                 return;
1994
1995         }
1996 }
1997
1998 static void
1999 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2000 {
2001         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
2002             UMASS_T_BBB_DATA_READ, error);
2003 }
2004
2005 static void
2006 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
2007 {
2008         struct umass_softc *sc = usbd_xfer_softc(xfer);
2009         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2010 #ifndef UMASS_EXT_BUFFER
2011         struct usb_page_cache *pc;
2012 #endif
2013         int actlen, sumlen;
2014
2015         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2016
2017         switch (USB_GET_STATE(xfer)) {
2018         case USB_ST_TRANSFERRED:
2019                 sc->sc_transfer.data_rem -= actlen;
2020                 sc->sc_transfer.data_ptr += actlen;
2021                 sc->sc_transfer.actlen += actlen;
2022
2023                 if (actlen < sumlen) {
2024                         /* short transfer */
2025                         sc->sc_transfer.data_rem = 0;
2026                 }
2027         case USB_ST_SETUP:
2028                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
2029                     max_bulk, sc->sc_transfer.data_rem);
2030
2031                 if (sc->sc_transfer.data_rem == 0) {
2032                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
2033                         return;
2034                 }
2035                 if (max_bulk > sc->sc_transfer.data_rem) {
2036                         max_bulk = sc->sc_transfer.data_rem;
2037                 }
2038                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2039
2040 #ifdef UMASS_EXT_BUFFER
2041                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2042                     max_bulk);
2043 #else
2044                 pc = usbd_xfer_get_frame(xfer, 0);
2045                 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
2046                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2047 #endif
2048
2049                 usbd_transfer_submit(xfer);
2050                 return;
2051
2052         default:                        /* Error */
2053                 if (error == USB_ERR_CANCELLED) {
2054                         umass_tr_error(xfer, error);
2055                 } else {
2056                         umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
2057                 }
2058                 return;
2059
2060         }
2061 }
2062
2063 static void
2064 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2065 {
2066         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
2067             UMASS_T_BBB_DATA_WRITE, error);
2068 }
2069
2070 static void
2071 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
2072 {
2073         struct umass_softc *sc = usbd_xfer_softc(xfer);
2074         union ccb *ccb = sc->sc_transfer.ccb;
2075         struct usb_page_cache *pc;
2076         uint32_t residue;
2077         int actlen;
2078
2079         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2080
2081         switch (USB_GET_STATE(xfer)) {
2082         case USB_ST_TRANSFERRED:
2083
2084                 /*
2085                  * Do a full reset if there is something wrong with the CSW:
2086                  */
2087                 sc->sc_status_try = 1;
2088
2089                 /* Zero missing parts of the CSW: */
2090
2091                 if (actlen < sizeof(sc->csw)) {
2092                         bzero(&sc->csw, sizeof(sc->csw));
2093                 }
2094                 pc = usbd_xfer_get_frame(xfer, 0);
2095                 usbd_copy_out(pc, 0, &sc->csw, actlen);
2096
2097                 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2098
2099                 residue = UGETDW(sc->csw.dCSWDataResidue);
2100
2101                 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
2102                         residue = (sc->sc_transfer.data_len -
2103                             sc->sc_transfer.actlen);
2104                 }
2105                 if (residue > sc->sc_transfer.data_len) {
2106                         DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
2107                             "to %d bytes\n", residue, sc->sc_transfer.data_len);
2108                         residue = sc->sc_transfer.data_len;
2109                 }
2110                 /* translate weird command-status signatures: */
2111                 if (sc->sc_quirks & WRONG_CSWSIG) {
2112
2113                         uint32_t temp = UGETDW(sc->csw.dCSWSignature);
2114
2115                         if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
2116                             (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
2117                                 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2118                         }
2119                 }
2120                 /* check CSW and handle eventual error */
2121                 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2122                         DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
2123                             UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
2124                         /*
2125                          * Invalid CSW: Wrong signature or wrong tag might
2126                          * indicate that we lost synchronization. Reset the
2127                          * device.
2128                          */
2129                         goto tr_error;
2130                 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
2131                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
2132                             "0x%08x\n", UGETDW(sc->csw.dCSWTag),
2133                             UGETDW(sc->cbw.dCBWTag));
2134                         goto tr_error;
2135                 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2136                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
2137                             sc->csw.bCSWStatus, CSWSTATUS_PHASE);
2138                         goto tr_error;
2139                 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2140                         DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
2141                             "%d\n", residue);
2142                         goto tr_error;
2143                 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
2144                         DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
2145                             sc->sc_transfer.actlen, sc->sc_transfer.data_len);
2146                         goto tr_error;
2147                 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2148                         DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
2149                             "%d\n", residue);
2150
2151                         sc->sc_transfer.ccb = NULL;
2152
2153                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2154
2155                         (sc->sc_transfer.callback)
2156                             (sc, ccb, residue, STATUS_CMD_FAILED);
2157                 } else {
2158                         sc->sc_transfer.ccb = NULL;
2159
2160                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2161
2162                         (sc->sc_transfer.callback)
2163                             (sc, ccb, residue, STATUS_CMD_OK);
2164                 }
2165                 return;
2166
2167         case USB_ST_SETUP:
2168                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2169                 usbd_transfer_submit(xfer);
2170                 return;
2171
2172         default:
2173 tr_error:
2174                 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
2175                     usbd_errstr(error), sc->sc_status_try);
2176
2177                 if ((error == USB_ERR_CANCELLED) ||
2178                     (sc->sc_status_try)) {
2179                         umass_tr_error(xfer, error);
2180                 } else {
2181                         sc->sc_status_try = 1;
2182                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
2183                 }
2184                 return;
2185
2186         }
2187 }
2188
2189 static void
2190 umass_command_start(struct umass_softc *sc, uint8_t dir,
2191     void *data_ptr, uint32_t data_len,
2192     uint32_t data_timeout, umass_callback_t *callback,
2193     union ccb *ccb)
2194 {
2195         sc->sc_transfer.lun = ccb->ccb_h.target_lun;
2196
2197         /*
2198          * NOTE: assumes that "sc->sc_transfer.cmd_data" and
2199          * "sc->sc_transfer.cmd_len" has been properly
2200          * initialized.
2201          */
2202
2203         sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
2204         sc->sc_transfer.data_ptr = data_ptr;
2205         sc->sc_transfer.data_len = data_len;
2206         sc->sc_transfer.data_rem = data_len;
2207         sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
2208
2209         sc->sc_transfer.actlen = 0;
2210         sc->sc_transfer.callback = callback;
2211         sc->sc_transfer.ccb = ccb;
2212
2213         if (sc->sc_xfer[sc->sc_last_xfer_index]) {
2214                 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
2215         } else {
2216                 ccb->ccb_h.status = CAM_TID_INVALID;
2217                 xpt_done(ccb);
2218         }
2219 }
2220
2221 static uint8_t
2222 umass_bbb_get_max_lun(struct umass_softc *sc)
2223 {
2224         struct usb_device_request req;
2225         usb_error_t err;
2226         uint8_t buf = 0;
2227
2228         /* The Get Max Lun command is a class-specific request. */
2229         req.bmRequestType = UT_READ_CLASS_INTERFACE;
2230         req.bRequest = UR_BBB_GET_MAX_LUN;
2231         USETW(req.wValue, 0);
2232         req.wIndex[0] = sc->sc_iface_no;
2233         req.wIndex[1] = 0;
2234         USETW(req.wLength, 1);
2235
2236         err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
2237         if (err) {
2238                 buf = 0;
2239
2240                 /* Device doesn't support Get Max Lun request. */
2241                 printf("%s: Get Max Lun not supported (%s)\n",
2242                     sc->sc_name, usbd_errstr(err));
2243         }
2244         return (buf);
2245 }
2246
2247 /*
2248  * Command/Bulk/Interrupt (CBI) specific functions
2249  */
2250
2251 static void
2252 umass_cbi_start_status(struct umass_softc *sc)
2253 {
2254         if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
2255                 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
2256         } else {
2257                 union ccb *ccb = sc->sc_transfer.ccb;
2258
2259                 sc->sc_transfer.ccb = NULL;
2260
2261                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2262
2263                 (sc->sc_transfer.callback)
2264                     (sc, ccb, (sc->sc_transfer.data_len -
2265                     sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
2266         }
2267 }
2268
2269 static void
2270 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
2271 {
2272         struct umass_softc *sc = usbd_xfer_softc(xfer);
2273         struct usb_device_request req;
2274         struct usb_page_cache *pc;
2275         uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
2276
2277         uint8_t i;
2278
2279         switch (USB_GET_STATE(xfer)) {
2280         case USB_ST_TRANSFERRED:
2281                 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
2282                 return;
2283
2284         case USB_ST_SETUP:
2285                 /*
2286                  * Command Block Reset Protocol
2287                  *
2288                  * First send a reset request to the device. Then clear
2289                  * any possibly stalled bulk endpoints.
2290                  *
2291                  * This is done in 3 steps, using 3 transfers:
2292                  * UMASS_T_CBI_RESET1
2293                  * UMASS_T_CBI_RESET2
2294                  * UMASS_T_CBI_RESET3
2295                  * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
2296                  */
2297
2298                 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
2299
2300                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2301                 req.bRequest = UR_CBI_ADSC;
2302                 USETW(req.wValue, 0);
2303                 req.wIndex[0] = sc->sc_iface_no;
2304                 req.wIndex[1] = 0;
2305                 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
2306
2307                 /*
2308                  * The 0x1d code is the SEND DIAGNOSTIC command. To
2309                  * distinguish between the two, the last 10 bytes of the CBL
2310                  * is filled with 0xff (section 2.2 of the CBI
2311                  * specification)
2312                  */
2313                 buf[0] = 0x1d;          /* Command Block Reset */
2314                 buf[1] = 0x04;
2315
2316                 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
2317                         buf[i] = 0xff;
2318                 }
2319
2320                 pc = usbd_xfer_get_frame(xfer, 0);
2321                 usbd_copy_in(pc, 0, &req, sizeof(req));
2322                 pc = usbd_xfer_get_frame(xfer, 1);
2323                 usbd_copy_in(pc, 0, buf, sizeof(buf));
2324
2325                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
2326                 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
2327                 usbd_xfer_set_frames(xfer, 2);
2328                 usbd_transfer_submit(xfer);
2329                 return;
2330
2331         default:                        /* Error */
2332                 umass_tr_error(xfer, error);
2333                 return;
2334
2335         }
2336 }
2337
2338 static void
2339 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
2340 {
2341         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
2342             UMASS_T_CBI_DATA_READ, error);
2343 }
2344
2345 static void
2346 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
2347 {
2348         struct umass_softc *sc = usbd_xfer_softc(xfer);
2349
2350         umass_t_cbi_data_clear_stall_callback
2351             (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
2352             sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
2353             UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
2354             UMASS_T_CBI_DATA_WRITE, error);
2355 }
2356
2357 static void
2358 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
2359 {
2360         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
2361             UMASS_T_CBI_STATUS, error);
2362 }
2363
2364 static void
2365 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
2366     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
2367 {
2368         struct umass_softc *sc = usbd_xfer_softc(xfer);
2369
2370         switch (USB_GET_STATE(xfer)) {
2371         case USB_ST_TRANSFERRED:
2372 tr_transferred:
2373                 if (next_xfer == UMASS_T_CBI_STATUS) {
2374                         umass_cbi_start_status(sc);
2375                 } else {
2376                         umass_transfer_start(sc, next_xfer);
2377                 }
2378                 return;
2379
2380         case USB_ST_SETUP:
2381                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
2382                         goto tr_transferred;    /* should not happen */
2383                 }
2384                 return;
2385
2386         default:                        /* Error */
2387                 umass_tr_error(xfer, error);
2388                 return;
2389
2390         }
2391 }
2392
2393 static void
2394 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
2395 {
2396         struct umass_softc *sc = usbd_xfer_softc(xfer);
2397         union ccb *ccb = sc->sc_transfer.ccb;
2398         struct usb_device_request req;
2399         struct usb_page_cache *pc;
2400
2401         switch (USB_GET_STATE(xfer)) {
2402         case USB_ST_TRANSFERRED:
2403
2404                 if (sc->sc_transfer.dir == DIR_NONE) {
2405                         umass_cbi_start_status(sc);
2406                 } else {
2407                         umass_transfer_start
2408                             (sc, (sc->sc_transfer.dir == DIR_IN) ?
2409                             UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
2410                 }
2411                 return;
2412
2413         case USB_ST_SETUP:
2414
2415                 if (ccb) {
2416
2417                         /*
2418                          * do a CBI transfer with cmd_len bytes from
2419                          * cmd_data, possibly a data phase of data_len
2420                          * bytes from/to the device and finally a status
2421                          * read phase.
2422                          */
2423
2424                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2425                         req.bRequest = UR_CBI_ADSC;
2426                         USETW(req.wValue, 0);
2427                         req.wIndex[0] = sc->sc_iface_no;
2428                         req.wIndex[1] = 0;
2429                         req.wLength[0] = sc->sc_transfer.cmd_len;
2430                         req.wLength[1] = 0;
2431
2432                         pc = usbd_xfer_get_frame(xfer, 0);
2433                         usbd_copy_in(pc, 0, &req, sizeof(req));
2434                         pc = usbd_xfer_get_frame(xfer, 1);
2435                         usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
2436                             sc->sc_transfer.cmd_len);
2437
2438                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
2439                         usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
2440                         usbd_xfer_set_frames(xfer,
2441                             sc->sc_transfer.cmd_len ? 2 : 1);
2442
2443                         DIF(UDMASS_CBI,
2444                             umass_cbi_dump_cmd(sc,
2445                             sc->sc_transfer.cmd_data,
2446                             sc->sc_transfer.cmd_len));
2447
2448                         usbd_transfer_submit(xfer);
2449                 }
2450                 return;
2451
2452         default:                        /* Error */
2453                 umass_tr_error(xfer, error);
2454                 return;
2455
2456         }
2457 }
2458
2459 static void
2460 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
2461 {
2462         struct umass_softc *sc = usbd_xfer_softc(xfer);
2463         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2464 #ifndef UMASS_EXT_BUFFER
2465         struct usb_page_cache *pc;
2466 #endif
2467         int actlen, sumlen;
2468
2469         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2470
2471         switch (USB_GET_STATE(xfer)) {
2472         case USB_ST_TRANSFERRED:
2473 #ifndef UMASS_EXT_BUFFER
2474                 pc = usbd_xfer_get_frame(xfer, 0);
2475                 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
2476 #endif
2477                 sc->sc_transfer.data_rem -= actlen;
2478                 sc->sc_transfer.data_ptr += actlen;
2479                 sc->sc_transfer.actlen += actlen;
2480
2481                 if (actlen < sumlen) {
2482                         /* short transfer */
2483                         sc->sc_transfer.data_rem = 0;
2484                 }
2485         case USB_ST_SETUP:
2486                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2487                     max_bulk, sc->sc_transfer.data_rem);
2488
2489                 if (sc->sc_transfer.data_rem == 0) {
2490                         umass_cbi_start_status(sc);
2491                         return;
2492                 }
2493                 if (max_bulk > sc->sc_transfer.data_rem) {
2494                         max_bulk = sc->sc_transfer.data_rem;
2495                 }
2496                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2497
2498 #ifdef UMASS_EXT_BUFFER
2499                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2500                     max_bulk);
2501 #else
2502                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2503 #endif
2504                 usbd_transfer_submit(xfer);
2505                 return;
2506
2507         default:                        /* Error */
2508                 if ((error == USB_ERR_CANCELLED) ||
2509                     (sc->sc_transfer.callback != &umass_cam_cb)) {
2510                         umass_tr_error(xfer, error);
2511                 } else {
2512                         umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
2513                 }
2514                 return;
2515
2516         }
2517 }
2518
2519 static void
2520 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2521 {
2522         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2523             UMASS_T_CBI_DATA_READ, error);
2524 }
2525
2526 static void
2527 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
2528 {
2529         struct umass_softc *sc = usbd_xfer_softc(xfer);
2530         uint32_t max_bulk = usbd_xfer_max_len(xfer);
2531 #ifndef UMASS_EXT_BUFFER
2532         struct usb_page_cache *pc;
2533 #endif
2534         int actlen, sumlen;
2535
2536         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2537
2538         switch (USB_GET_STATE(xfer)) {
2539         case USB_ST_TRANSFERRED:
2540                 sc->sc_transfer.data_rem -= actlen;
2541                 sc->sc_transfer.data_ptr += actlen;
2542                 sc->sc_transfer.actlen += actlen;
2543
2544                 if (actlen < sumlen) {
2545                         /* short transfer */
2546                         sc->sc_transfer.data_rem = 0;
2547                 }
2548         case USB_ST_SETUP:
2549                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2550                     max_bulk, sc->sc_transfer.data_rem);
2551
2552                 if (sc->sc_transfer.data_rem == 0) {
2553                         umass_cbi_start_status(sc);
2554                         return;
2555                 }
2556                 if (max_bulk > sc->sc_transfer.data_rem) {
2557                         max_bulk = sc->sc_transfer.data_rem;
2558                 }
2559                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
2560
2561 #ifdef UMASS_EXT_BUFFER
2562                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
2563                     max_bulk);
2564 #else
2565                 pc = usbd_xfer_get_frame(xfer, 0);
2566                 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
2567                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
2568 #endif
2569
2570                 usbd_transfer_submit(xfer);
2571                 return;
2572
2573         default:                        /* Error */
2574                 if ((error == USB_ERR_CANCELLED) ||
2575                     (sc->sc_transfer.callback != &umass_cam_cb)) {
2576                         umass_tr_error(xfer, error);
2577                 } else {
2578                         umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
2579                 }
2580                 return;
2581
2582         }
2583 }
2584
2585 static void
2586 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2587 {
2588         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2589             UMASS_T_CBI_DATA_WRITE, error);
2590 }
2591
2592 static void
2593 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
2594 {
2595         struct umass_softc *sc = usbd_xfer_softc(xfer);
2596         union ccb *ccb = sc->sc_transfer.ccb;
2597         struct usb_page_cache *pc;
2598         uint32_t residue;
2599         uint8_t status;
2600         int actlen;
2601
2602         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2603
2604         switch (USB_GET_STATE(xfer)) {
2605         case USB_ST_TRANSFERRED:
2606
2607                 if (actlen < sizeof(sc->sbl)) {
2608                         goto tr_setup;
2609                 }
2610                 pc = usbd_xfer_get_frame(xfer, 0);
2611                 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
2612
2613                 residue = (sc->sc_transfer.data_len -
2614                     sc->sc_transfer.actlen);
2615
2616                 /* dissect the information in the buffer */
2617
2618                 if (sc->sc_proto & UMASS_PROTO_UFI) {
2619
2620                         /*
2621                          * Section 3.4.3.1.3 specifies that the UFI command
2622                          * protocol returns an ASC and ASCQ in the interrupt
2623                          * data block.
2624                          */
2625
2626                         DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2627                             "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2628                             sc->sbl.ufi.ascq);
2629
2630                         status = (((sc->sbl.ufi.asc == 0) &&
2631                             (sc->sbl.ufi.ascq == 0)) ?
2632                             STATUS_CMD_OK : STATUS_CMD_FAILED);
2633
2634                         sc->sc_transfer.ccb = NULL;
2635
2636                         sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2637
2638                         (sc->sc_transfer.callback)
2639                             (sc, ccb, residue, status);
2640
2641                         return;
2642
2643                 } else {
2644
2645                         /* Command Interrupt Data Block */
2646
2647                         DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2648                             sc->sbl.common.type, sc->sbl.common.value);
2649
2650                         if (sc->sbl.common.type == IDB_TYPE_CCI) {
2651
2652                                 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2653
2654                                 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2655                                     (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2656                                     (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2657                                     STATUS_WIRE_FAILED);
2658
2659                                 sc->sc_transfer.ccb = NULL;
2660
2661                                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2662
2663                                 (sc->sc_transfer.callback)
2664                                     (sc, ccb, residue, status);
2665
2666                                 return;
2667                         }
2668                 }
2669
2670                 /* fallthrough */
2671
2672         case USB_ST_SETUP:
2673 tr_setup:
2674                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2675                 usbd_transfer_submit(xfer);
2676                 return;
2677
2678         default:                        /* Error */
2679                 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2680                     usbd_errstr(error));
2681                 umass_tr_error(xfer, error);
2682                 return;
2683
2684         }
2685 }
2686
2687 /*
2688  * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2689  */
2690
2691 static int
2692 umass_cam_attach_sim(struct umass_softc *sc)
2693 {
2694         struct cam_devq *devq;          /* Per device Queue */
2695
2696         /*
2697          * A HBA is attached to the CAM layer.
2698          *
2699          * The CAM layer will then after a while start probing for devices on
2700          * the bus. The number of SIMs is limited to one.
2701          */
2702
2703         devq = cam_simq_alloc(1 /* maximum openings */ );
2704         if (devq == NULL) {
2705                 return (ENOMEM);
2706         }
2707         sc->sc_sim = cam_sim_alloc
2708             (&umass_cam_action, &umass_cam_poll,
2709             DEVNAME_SIM,
2710             sc /* priv */ ,
2711             sc->sc_unit /* unit number */ ,
2712 #if (__FreeBSD_version >= 700037)
2713             &sc->sc_mtx /* mutex */ ,
2714 #endif
2715             1 /* maximum device openings */ ,
2716             0 /* maximum tagged device openings */ ,
2717             devq);
2718
2719         if (sc->sc_sim == NULL) {
2720                 cam_simq_free(devq);
2721                 return (ENOMEM);
2722         }
2723
2724 #if (__FreeBSD_version >= 700037)
2725         mtx_lock(&sc->sc_mtx);
2726 #endif
2727
2728 #if (__FreeBSD_version >= 700048)
2729         if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2730                 mtx_unlock(&sc->sc_mtx);
2731                 return (ENOMEM);
2732         }
2733 #else
2734         if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2735 #if (__FreeBSD_version >= 700037)
2736                 mtx_unlock(&sc->sc_mtx);
2737 #endif
2738                 return (ENOMEM);
2739         }
2740 #endif
2741
2742 #if (__FreeBSD_version >= 700037)
2743         mtx_unlock(&sc->sc_mtx);
2744 #endif
2745         return (0);
2746 }
2747
2748 static void
2749 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2750 {
2751 #if USB_DEBUG
2752         struct umass_softc *sc = NULL;
2753
2754         if (ccb->ccb_h.status != CAM_REQ_CMP) {
2755                 DPRINTF(sc, UDMASS_SCSI, "%s:%d Rescan failed, 0x%04x\n",
2756                     periph->periph_name, periph->unit_number,
2757                     ccb->ccb_h.status);
2758         } else {
2759                 DPRINTF(sc, UDMASS_SCSI, "%s%d: Rescan succeeded\n",
2760                     periph->periph_name, periph->unit_number);
2761         }
2762 #endif
2763
2764         xpt_free_path(ccb->ccb_h.path);
2765         free(ccb, M_USBDEV);
2766 }
2767
2768 static void
2769 umass_cam_rescan(struct umass_softc *sc)
2770 {
2771         struct cam_path *path;
2772         union ccb *ccb;
2773
2774         DPRINTF(sc, UDMASS_SCSI, "scbus%d: scanning for %d:%d:%d\n",
2775             cam_sim_path(sc->sc_sim),
2776             cam_sim_path(sc->sc_sim),
2777             sc->sc_unit, CAM_LUN_WILDCARD);
2778
2779         ccb = malloc(sizeof(*ccb), M_USBDEV, M_WAITOK | M_ZERO);
2780
2781         if (ccb == NULL) {
2782                 return;
2783         }
2784 #if (__FreeBSD_version >= 700037)
2785         mtx_lock(&sc->sc_mtx);
2786 #endif
2787
2788         if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sc_sim),
2789             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2790             != CAM_REQ_CMP) {
2791 #if (__FreeBSD_version >= 700037)
2792                 mtx_unlock(&sc->sc_mtx);
2793 #endif
2794                 free(ccb, M_USBDEV);
2795                 return;
2796         }
2797         xpt_setup_ccb(&ccb->ccb_h, path, 5 /* priority (low) */ );
2798         ccb->ccb_h.func_code = XPT_SCAN_BUS;
2799         ccb->ccb_h.cbfcnp = &umass_cam_rescan_callback;
2800         ccb->crcn.flags = CAM_FLAG_NONE;
2801         xpt_action(ccb);
2802
2803 #if (__FreeBSD_version >= 700037)
2804         mtx_unlock(&sc->sc_mtx);
2805 #endif
2806
2807         /* The scan is in progress now. */
2808 }
2809
2810 static void
2811 umass_cam_attach(struct umass_softc *sc)
2812 {
2813 #ifndef USB_DEBUG
2814         if (bootverbose)
2815 #endif
2816                 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2817                     sc->sc_name, cam_sim_path(sc->sc_sim),
2818                     sc->sc_unit, CAM_LUN_WILDCARD,
2819                     cam_sim_path(sc->sc_sim));
2820
2821         if (!cold) {
2822                 /*
2823                  * Notify CAM of the new device after a short delay. Any
2824                  * failure is benign, as the user can still do it by hand
2825                  * (camcontrol rescan <busno>). Only do this if we are not
2826                  * booting, because CAM does a scan after booting has
2827                  * completed, when interrupts have been enabled.
2828                  */
2829
2830                 /* scan the new sim */
2831                 umass_cam_rescan(sc);
2832         }
2833 }
2834
2835 /* umass_cam_detach
2836  *      detach from the CAM layer
2837  */
2838
2839 static void
2840 umass_cam_detach_sim(struct umass_softc *sc)
2841 {
2842         if (sc->sc_sim != NULL) {
2843                 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2844                         /* accessing the softc is not possible after this */
2845                         sc->sc_sim->softc = UMASS_GONE;
2846                         cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2847                 } else {
2848                         panic("%s: CAM layer is busy!\n",
2849                             sc->sc_name);
2850                 }
2851                 sc->sc_sim = NULL;
2852         }
2853 }
2854
2855 /* umass_cam_action
2856  *      CAM requests for action come through here
2857  */
2858
2859 static void
2860 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2861 {
2862         struct umass_softc *sc = (struct umass_softc *)sim->softc;
2863
2864         if (sc == UMASS_GONE ||
2865             (sc != NULL && !usbd_device_attached(sc->sc_udev))) {
2866                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2867                 xpt_done(ccb);
2868                 return;
2869         }
2870         if (sc) {
2871 #if (__FreeBSD_version < 700037)
2872                 mtx_lock(&sc->sc_mtx);
2873 #endif
2874         }
2875         /*
2876          * Verify, depending on the operation to perform, that we either got
2877          * a valid sc, because an existing target was referenced, or
2878          * otherwise the SIM is addressed.
2879          *
2880          * This avoids bombing out at a printf and does give the CAM layer some
2881          * sensible feedback on errors.
2882          */
2883         switch (ccb->ccb_h.func_code) {
2884         case XPT_SCSI_IO:
2885         case XPT_RESET_DEV:
2886         case XPT_GET_TRAN_SETTINGS:
2887         case XPT_SET_TRAN_SETTINGS:
2888         case XPT_CALC_GEOMETRY:
2889                 /* the opcodes requiring a target. These should never occur. */
2890                 if (sc == NULL) {
2891                         DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2892                             "Invalid target (target needed)\n",
2893                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2894                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2895                             ccb->ccb_h.func_code);
2896
2897                         ccb->ccb_h.status = CAM_TID_INVALID;
2898                         xpt_done(ccb);
2899                         goto done;
2900                 }
2901                 break;
2902         case XPT_PATH_INQ:
2903         case XPT_NOOP:
2904                 /*
2905                  * The opcodes sometimes aimed at a target (sc is valid),
2906                  * sometimes aimed at the SIM (sc is invalid and target is
2907                  * CAM_TARGET_WILDCARD)
2908                  */
2909                 if ((sc == NULL) &&
2910                     (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2911                         DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2912                             "Invalid target (no wildcard)\n",
2913                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2914                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2915                             ccb->ccb_h.func_code);
2916
2917                         ccb->ccb_h.status = CAM_TID_INVALID;
2918                         xpt_done(ccb);
2919                         goto done;
2920                 }
2921                 break;
2922         default:
2923                 /* XXX Hm, we should check the input parameters */
2924                 break;
2925         }
2926
2927         /* Perform the requested action */
2928         switch (ccb->ccb_h.func_code) {
2929         case XPT_SCSI_IO:
2930                 {
2931                         uint8_t *cmd;
2932                         uint8_t dir;
2933
2934                         if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2935                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2936                         } else {
2937                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2938                         }
2939
2940                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2941                             "cmd: 0x%02x, flags: 0x%02x, "
2942                             "%db cmd/%db data/%db sense\n",
2943                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2944                             ccb->ccb_h.target_lun, cmd[0],
2945                             ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2946                             ccb->csio.dxfer_len, ccb->csio.sense_len);
2947
2948                         if (sc->sc_transfer.ccb) {
2949                                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2950                                     "I/O in progress, deferring\n",
2951                                     cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2952                                     ccb->ccb_h.target_lun);
2953                                 ccb->ccb_h.status = CAM_SCSI_BUSY;
2954                                 xpt_done(ccb);
2955                                 goto done;
2956                         }
2957                         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2958                         case CAM_DIR_IN:
2959                                 dir = DIR_IN;
2960                                 break;
2961                         case CAM_DIR_OUT:
2962                                 dir = DIR_OUT;
2963                                 DIF(UDMASS_SCSI,
2964                                     umass_dump_buffer(sc, ccb->csio.data_ptr,
2965                                     ccb->csio.dxfer_len, 48));
2966                                 break;
2967                         default:
2968                                 dir = DIR_NONE;
2969                         }
2970
2971                         ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2972
2973                         /*
2974                          * sc->sc_transform will convert the command to the
2975                          * command format needed by the specific command set
2976                          * and return the converted command in
2977                          * "sc->sc_transfer.cmd_data"
2978                          */
2979                         if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2980
2981                                 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2982
2983                                         /*
2984                                          * Umass devices don't generally report their serial numbers
2985                                          * in the usual SCSI way.  Emulate it here.
2986                                          */
2987                                         if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2988                                             sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER &&
2989                                             sc->sc_udev != NULL &&
2990                                             sc->sc_udev->serial != NULL &&
2991                                             sc->sc_udev->serial[0] != '\0') {
2992                                                 struct scsi_vpd_unit_serial_number *vpd_serial;
2993
2994                                                 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2995                                                 vpd_serial->length = strlen(sc->sc_udev->serial);
2996                                                 if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2997                                                         vpd_serial->length = sizeof(vpd_serial->serial_num);
2998                                                 memcpy(vpd_serial->serial_num, sc->sc_udev->serial, vpd_serial->length);
2999                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3000                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3001                                                 xpt_done(ccb);
3002                                                 goto done;
3003                                         }
3004
3005                                         /*
3006                                          * Handle EVPD inquiry for broken devices first
3007                                          * NO_INQUIRY also implies NO_INQUIRY_EVPD
3008                                          */
3009                                         if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
3010                                             (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
3011                                                 struct scsi_sense_data *sense;
3012
3013                                                 sense = &ccb->csio.sense_data;
3014                                                 bzero(sense, sizeof(*sense));
3015                                                 sense->error_code = SSD_CURRENT_ERROR;
3016                                                 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
3017                                                 sense->add_sense_code = 0x24;
3018                                                 sense->extra_len = 10;
3019                                                 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3020                                                 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
3021                                                     CAM_AUTOSNS_VALID;
3022                                                 xpt_done(ccb);
3023                                                 goto done;
3024                                         }
3025                                         /*
3026                                          * Return fake inquiry data for
3027                                          * broken devices
3028                                          */
3029                                         if (sc->sc_quirks & NO_INQUIRY) {
3030                                                 memcpy(ccb->csio.data_ptr, &fake_inq_data,
3031                                                     sizeof(fake_inq_data));
3032                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3033                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3034                                                 xpt_done(ccb);
3035                                                 goto done;
3036                                         }
3037                                         if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3038                                                 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
3039                                         }
3040                                 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
3041                                         if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
3042                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3043                                                 ccb->ccb_h.status = CAM_REQ_CMP;
3044                                                 xpt_done(ccb);
3045                                                 goto done;
3046                                         }
3047                                 }
3048                                 umass_command_start(sc, dir, ccb->csio.data_ptr,
3049                                     ccb->csio.dxfer_len,
3050                                     ccb->ccb_h.timeout,
3051                                     &umass_cam_cb, ccb);
3052                         }
3053                         break;
3054                 }
3055         case XPT_PATH_INQ:
3056                 {
3057                         struct ccb_pathinq *cpi = &ccb->cpi;
3058
3059                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
3060                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3061                             ccb->ccb_h.target_lun);
3062
3063                         /* host specific information */
3064                         cpi->version_num = 1;
3065                         cpi->hba_inquiry = 0;
3066                         cpi->target_sprt = 0;
3067                         cpi->hba_misc = PIM_NO_6_BYTE;
3068                         cpi->hba_eng_cnt = 0;
3069                         cpi->max_target = UMASS_SCSIID_MAX;     /* one target */
3070                         cpi->initiator_id = UMASS_SCSIID_HOST;
3071                         strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3072                         strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
3073                         strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3074                         cpi->unit_number = cam_sim_unit(sim);
3075                         cpi->bus_id = sc->sc_unit;
3076 #if (__FreeBSD_version >= 700025)
3077                         cpi->protocol = PROTO_SCSI;
3078                         cpi->protocol_version = SCSI_REV_2;
3079                         cpi->transport = XPORT_USB;
3080                         cpi->transport_version = 0;
3081 #endif
3082                         if (sc == NULL) {
3083                                 cpi->base_transfer_speed = 0;
3084                                 cpi->max_lun = 0;
3085                         } else {
3086                                 if (sc->sc_quirks & FLOPPY_SPEED) {
3087                                         cpi->base_transfer_speed =
3088                                             UMASS_FLOPPY_TRANSFER_SPEED;
3089                                 } else if (usbd_get_speed(sc->sc_udev) ==
3090                                     USB_SPEED_HIGH) {
3091                                         cpi->base_transfer_speed =
3092                                             UMASS_HIGH_TRANSFER_SPEED;
3093                                 } else {
3094                                         cpi->base_transfer_speed =
3095                                             UMASS_FULL_TRANSFER_SPEED;
3096                                 }
3097                                 cpi->max_lun = sc->sc_maxlun;
3098                         }
3099
3100                         cpi->ccb_h.status = CAM_REQ_CMP;
3101                         xpt_done(ccb);
3102                         break;
3103                 }
3104         case XPT_RESET_DEV:
3105                 {
3106                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
3107                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3108                             ccb->ccb_h.target_lun);
3109
3110                         umass_reset(sc);
3111
3112                         ccb->ccb_h.status = CAM_REQ_CMP;
3113                         xpt_done(ccb);
3114                         break;
3115                 }
3116         case XPT_GET_TRAN_SETTINGS:
3117                 {
3118                         struct ccb_trans_settings *cts = &ccb->cts;
3119
3120                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
3121                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3122                             ccb->ccb_h.target_lun);
3123
3124 #if (__FreeBSD_version >= 700025)
3125                         cts->protocol = PROTO_SCSI;
3126                         cts->protocol_version = SCSI_REV_2;
3127                         cts->transport = XPORT_USB;
3128                         cts->transport_version = 0;
3129                         cts->xport_specific.valid = 0;
3130 #else
3131                         cts->valid = 0;
3132                         cts->flags = 0; /* no disconnection, tagging */
3133 #endif
3134                         ccb->ccb_h.status = CAM_REQ_CMP;
3135                         xpt_done(ccb);
3136                         break;
3137                 }
3138         case XPT_SET_TRAN_SETTINGS:
3139                 {
3140                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3141                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3142                             ccb->ccb_h.target_lun);
3143
3144                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3145                         xpt_done(ccb);
3146                         break;
3147                 }
3148         case XPT_CALC_GEOMETRY:
3149                 {
3150                         cam_calc_geometry(&ccb->ccg, /* extended */ 1);
3151                         xpt_done(ccb);
3152                         break;
3153                 }
3154         case XPT_NOOP:
3155                 {
3156                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
3157                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3158                             ccb->ccb_h.target_lun);
3159
3160                         ccb->ccb_h.status = CAM_REQ_CMP;
3161                         xpt_done(ccb);
3162                         break;
3163                 }
3164         default:
3165                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
3166                     "Not implemented\n",
3167                     sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3168                     ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
3169
3170                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3171                 xpt_done(ccb);
3172                 break;
3173         }
3174
3175 done:
3176 #if (__FreeBSD_version < 700037)
3177         if (sc) {
3178                 mtx_unlock(&sc->sc_mtx);
3179         }
3180 #endif
3181         return;
3182 }
3183
3184 static void
3185 umass_cam_poll(struct cam_sim *sim)
3186 {
3187         struct umass_softc *sc = (struct umass_softc *)sim->softc;
3188
3189         if (sc == UMASS_GONE)
3190                 return;
3191
3192         DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
3193
3194         usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
3195 }
3196
3197
3198 /* umass_cam_cb
3199  *      finalise a completed CAM command
3200  */
3201
3202 static void
3203 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3204     uint8_t status)
3205 {
3206         ccb->csio.resid = residue;
3207
3208         switch (status) {
3209         case STATUS_CMD_OK:
3210                 ccb->ccb_h.status = CAM_REQ_CMP;
3211                 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
3212                     (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3213                     (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3214                         struct scsi_read_capacity_data *rcap;
3215                         uint32_t maxsector;
3216
3217                         rcap = (void *)(ccb->csio.data_ptr);
3218                         maxsector = scsi_4btoul(rcap->addr) - 1;
3219                         scsi_ulto4b(maxsector, rcap->addr);
3220                 }
3221                 /*
3222                  * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
3223                  * of pages supported by the device - otherwise, CAM
3224                  * will never ask us for the serial number if the
3225                  * device cannot handle that by itself.
3226                  */
3227                 if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
3228                     sc->sc_transfer.cmd_data[0] == INQUIRY &&
3229                     (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
3230                     sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
3231                     sc->sc_udev != NULL &&
3232                     sc->sc_udev->serial != NULL &&
3233                     sc->sc_udev->serial[0] != '\0') {
3234                         struct ccb_scsiio *csio;
3235                         struct scsi_vpd_supported_page_list *page_list;
3236
3237                         csio = &ccb->csio;
3238                         page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
3239                         if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
3240                                 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
3241                                 page_list->length++;
3242                         }
3243                 }
3244                 xpt_done(ccb);
3245                 break;
3246
3247         case STATUS_CMD_UNKNOWN:
3248         case STATUS_CMD_FAILED:
3249
3250                 /* fetch sense data */
3251
3252                 /* the rest of the command was filled in at attach */
3253                 sc->cam_scsi_sense.length = ccb->csio.sense_len;
3254
3255                 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
3256                     "sense data\n", ccb->csio.sense_len);
3257
3258                 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
3259                     sizeof(sc->cam_scsi_sense))) {
3260
3261                         if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
3262                             (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
3263                                 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
3264                         }
3265                         umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
3266                             ccb->csio.sense_len, ccb->ccb_h.timeout,
3267                             &umass_cam_sense_cb, ccb);
3268                 }
3269                 break;
3270
3271         default:
3272                 /*
3273                  * the wire protocol failed and will have recovered
3274                  * (hopefully).  We return an error to CAM and let CAM retry
3275                  * the command if necessary.
3276                  */
3277                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3278                 xpt_done(ccb);
3279                 break;
3280         }
3281 }
3282
3283 /*
3284  * Finalise a completed autosense operation
3285  */
3286 static void
3287 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3288     uint8_t status)
3289 {
3290         uint8_t *cmd;
3291         uint8_t key;
3292
3293         switch (status) {
3294         case STATUS_CMD_OK:
3295         case STATUS_CMD_UNKNOWN:
3296         case STATUS_CMD_FAILED:
3297
3298                 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
3299                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
3300                 } else {
3301                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
3302                 }
3303
3304                 key = (ccb->csio.sense_data.flags & SSD_KEY);
3305
3306                 /*
3307                  * Getting sense data always succeeds (apart from wire
3308                  * failures):
3309                  */
3310                 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3311                     (cmd[0] == INQUIRY) &&
3312                     (key == SSD_KEY_UNIT_ATTENTION)) {
3313                         /*
3314                          * Ignore unit attention errors in the case where
3315                          * the Unit Attention state is not cleared on
3316                          * REQUEST SENSE. They will appear again at the next
3317                          * command.
3318                          */
3319                         ccb->ccb_h.status = CAM_REQ_CMP;
3320                 } else if (key == SSD_KEY_NO_SENSE) {
3321                         /*
3322                          * No problem after all (in the case of CBI without
3323                          * CCI)
3324                          */
3325                         ccb->ccb_h.status = CAM_REQ_CMP;
3326                 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3327                             (cmd[0] == READ_CAPACITY) &&
3328                     (key == SSD_KEY_UNIT_ATTENTION)) {
3329                         /*
3330                          * Some devices do not clear the unit attention error
3331                          * on request sense. We insert a test unit ready
3332                          * command to make sure we clear the unit attention
3333                          * condition, then allow the retry to proceed as
3334                          * usual.
3335                          */
3336
3337                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3338                             | CAM_AUTOSNS_VALID;
3339                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3340
3341 #if 0
3342                         DELAY(300000);
3343 #endif
3344                         DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
3345                             "TEST_UNIT_READY\n");
3346
3347                         /* the rest of the command was filled in at attach */
3348
3349                         if (umass_std_transform(sc, ccb,
3350                             &sc->cam_scsi_test_unit_ready.opcode,
3351                             sizeof(sc->cam_scsi_test_unit_ready))) {
3352                                 umass_command_start(sc, DIR_NONE, NULL, 0,
3353                                     ccb->ccb_h.timeout,
3354                                     &umass_cam_quirk_cb, ccb);
3355                         }
3356                         break;
3357                 } else {
3358                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3359                             | CAM_AUTOSNS_VALID;
3360                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3361                 }
3362                 xpt_done(ccb);
3363                 break;
3364
3365         default:
3366                 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
3367                     "status %d\n", status);
3368                 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3369                 xpt_done(ccb);
3370         }
3371 }
3372
3373 /*
3374  * This completion code just handles the fact that we sent a test-unit-ready
3375  * after having previously failed a READ CAPACITY with CHECK_COND.  Even
3376  * though this command succeeded, we have to tell CAM to retry.
3377  */
3378 static void
3379 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3380     uint8_t status)
3381 {
3382         DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
3383             "returned status %d\n", status);
3384
3385         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3386             | CAM_AUTOSNS_VALID;
3387         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3388         xpt_done(ccb);
3389 }
3390
3391 /*
3392  * SCSI specific functions
3393  */
3394
3395 static uint8_t
3396 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3397     uint8_t cmd_len)
3398 {
3399         if ((cmd_len == 0) ||
3400             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3401                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3402                     "length: %d bytes\n", cmd_len);
3403                 return (0);             /* failure */
3404         }
3405         sc->sc_transfer.cmd_len = cmd_len;
3406
3407         switch (cmd_ptr[0]) {
3408         case TEST_UNIT_READY:
3409                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3410                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3411                             "to START_UNIT\n");
3412                         bzero(sc->sc_transfer.cmd_data, cmd_len);
3413                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3414                         sc->sc_transfer.cmd_data[4] = SSS_START;
3415                         return (1);
3416                 }
3417                 break;
3418
3419         case INQUIRY:
3420                 /*
3421                  * some drives wedge when asked for full inquiry
3422                  * information.
3423                  */
3424                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3425                         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3426                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3427                         return (1);
3428                 }
3429                 break;
3430         }
3431
3432         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3433         return (1);
3434 }
3435
3436 static uint8_t
3437 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
3438 {
3439         if ((cmd_len == 0) ||
3440             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3441                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3442                     "length: %d bytes\n", cmd_len);
3443                 return (0);             /* failure */
3444         }
3445         switch (cmd_ptr[0]) {
3446                 /* these commands are defined in RBC: */
3447         case READ_10:
3448         case READ_CAPACITY:
3449         case START_STOP_UNIT:
3450         case SYNCHRONIZE_CACHE:
3451         case WRITE_10:
3452         case 0x2f:                      /* VERIFY_10 is absent from
3453                                          * scsi_all.h??? */
3454         case INQUIRY:
3455         case MODE_SELECT_10:
3456         case MODE_SENSE_10:
3457         case TEST_UNIT_READY:
3458         case WRITE_BUFFER:
3459                 /*
3460                  * The following commands are not listed in my copy of the
3461                  * RBC specs. CAM however seems to want those, and at least
3462                  * the Sony DSC device appears to support those as well
3463                  */
3464         case REQUEST_SENSE:
3465         case PREVENT_ALLOW:
3466
3467                 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3468
3469                 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
3470                         bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len);
3471                         cmd_len = 12;
3472                 }
3473                 sc->sc_transfer.cmd_len = cmd_len;
3474                 return (1);             /* sucess */
3475
3476                 /* All other commands are not legal in RBC */
3477         default:
3478                 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
3479                     "command 0x%02x\n", cmd_ptr[0]);
3480                 return (0);             /* failure */
3481         }
3482 }
3483
3484 static uint8_t
3485 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3486     uint8_t cmd_len)
3487 {
3488         if ((cmd_len == 0) ||
3489             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3490                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3491                     "length: %d bytes\n", cmd_len);
3492                 return (0);             /* failure */
3493         }
3494         /* An UFI command is always 12 bytes in length */
3495         sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
3496
3497         /* Zero the command data */
3498         bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH);
3499
3500         switch (cmd_ptr[0]) {
3501                 /*
3502                  * Commands of which the format has been verified. They
3503                  * should work. Copy the command into the (zeroed out)
3504                  * destination buffer.
3505                  */
3506         case TEST_UNIT_READY:
3507                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3508                         /*
3509                          * Some devices do not support this command. Start
3510                          * Stop Unit should give the same results
3511                          */
3512                         DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
3513                             "to START_UNIT\n");
3514
3515                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3516                         sc->sc_transfer.cmd_data[4] = SSS_START;
3517                         return (1);
3518                 }
3519                 break;
3520
3521         case REZERO_UNIT:
3522         case REQUEST_SENSE:
3523         case FORMAT_UNIT:
3524         case INQUIRY:
3525         case START_STOP_UNIT:
3526         case SEND_DIAGNOSTIC:
3527         case PREVENT_ALLOW:
3528         case READ_CAPACITY:
3529         case READ_10:
3530         case WRITE_10:
3531         case POSITION_TO_ELEMENT:       /* SEEK_10 */
3532         case WRITE_AND_VERIFY:
3533         case VERIFY:
3534         case MODE_SELECT_10:
3535         case MODE_SENSE_10:
3536         case READ_12:
3537         case WRITE_12:
3538         case READ_FORMAT_CAPACITIES:
3539                 break;
3540
3541                 /*
3542                  * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3543                  * required for UFI devices, so it is appropriate to fake
3544                  * success.
3545                  */
3546         case SYNCHRONIZE_CACHE:
3547                 return (2);
3548
3549         default:
3550                 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
3551                     "command 0x%02x\n", cmd_ptr[0]);
3552                 return (0);             /* failure */
3553         }
3554
3555         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3556         return (1);                     /* success */
3557 }
3558
3559 /*
3560  * 8070i (ATAPI) specific functions
3561  */
3562 static uint8_t
3563 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3564     uint8_t cmd_len)
3565 {
3566         if ((cmd_len == 0) ||
3567             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3568                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3569                     "length: %d bytes\n", cmd_len);
3570                 return (0);             /* failure */
3571         }
3572         /* An ATAPI command is always 12 bytes in length. */
3573         sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
3574
3575         /* Zero the command data */
3576         bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH);
3577
3578         switch (cmd_ptr[0]) {
3579                 /*
3580                  * Commands of which the format has been verified. They
3581                  * should work. Copy the command into the destination
3582                  * buffer.
3583                  */
3584         case INQUIRY:
3585                 /*
3586                  * some drives wedge when asked for full inquiry
3587                  * information.
3588                  */
3589                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3590                         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3591
3592                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3593                         return (1);
3594                 }
3595                 break;
3596
3597         case TEST_UNIT_READY:
3598                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3599                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3600                             "to START_UNIT\n");
3601                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3602                         sc->sc_transfer.cmd_data[4] = SSS_START;
3603                         return (1);
3604                 }
3605                 break;
3606
3607         case REZERO_UNIT:
3608         case REQUEST_SENSE:
3609         case START_STOP_UNIT:
3610         case SEND_DIAGNOSTIC:
3611         case PREVENT_ALLOW:
3612         case READ_CAPACITY:
3613         case READ_10:
3614         case WRITE_10:
3615         case POSITION_TO_ELEMENT:       /* SEEK_10 */
3616         case SYNCHRONIZE_CACHE:
3617         case MODE_SELECT_10:
3618         case MODE_SENSE_10:
3619         case READ_BUFFER:
3620         case 0x42:                      /* READ_SUBCHANNEL */
3621         case 0x43:                      /* READ_TOC */
3622         case 0x44:                      /* READ_HEADER */
3623         case 0x47:                      /* PLAY_MSF (Play Minute/Second/Frame) */
3624         case 0x48:                      /* PLAY_TRACK */
3625         case 0x49:                      /* PLAY_TRACK_REL */
3626         case 0x4b:                      /* PAUSE */
3627         case 0x51:                      /* READ_DISK_INFO */
3628         case 0x52:                      /* READ_TRACK_INFO */
3629         case 0x54:                      /* SEND_OPC */
3630         case 0x59:                      /* READ_MASTER_CUE */
3631         case 0x5b:                      /* CLOSE_TR_SESSION */
3632         case 0x5c:                      /* READ_BUFFER_CAP */
3633         case 0x5d:                      /* SEND_CUE_SHEET */
3634         case 0xa1:                      /* BLANK */
3635         case 0xa5:                      /* PLAY_12 */
3636         case 0xa6:                      /* EXCHANGE_MEDIUM */
3637         case 0xad:                      /* READ_DVD_STRUCTURE */
3638         case 0xbb:                      /* SET_CD_SPEED */
3639         case 0xe5:                      /* READ_TRACK_INFO_PHILIPS */
3640                 break;;
3641
3642         case READ_12:
3643         case WRITE_12:
3644         default:
3645                 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
3646                     "command 0x%02x - trying anyway\n",
3647                     cmd_ptr[0]);
3648                 break;;
3649         }
3650
3651         bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3652         return (1);                     /* success */
3653 }
3654
3655 static uint8_t
3656 umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3657     uint8_t cmdlen)
3658 {
3659         return (0);                     /* failure */
3660 }
3661
3662 static uint8_t
3663 umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3664     uint8_t *cmd, uint8_t cmdlen)
3665 {
3666         uint8_t retval;
3667
3668         retval = (sc->sc_transform) (sc, cmd, cmdlen);
3669
3670         if (retval == 2) {
3671                 ccb->ccb_h.status = CAM_REQ_CMP;
3672                 xpt_done(ccb);
3673                 return (0);
3674         } else if (retval == 0) {
3675                 ccb->ccb_h.status = CAM_REQ_INVALID;
3676                 xpt_done(ccb);
3677                 return (0);
3678         }
3679         /* Command should be executed */
3680         return (1);
3681 }
3682
3683 #if USB_DEBUG
3684 static void
3685 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3686 {
3687         uint8_t *c = cbw->CBWCDB;
3688
3689         uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3690         uint32_t tag = UGETDW(cbw->dCBWTag);
3691
3692         uint8_t clen = cbw->bCDBLength;
3693         uint8_t flags = cbw->bCBWFlags;
3694         uint8_t lun = cbw->bCBWLUN;
3695
3696         DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3697             "(0x%02x%02x%02x%02x%02x%02x%s), "
3698             "data = %db, lun = %d, dir = %s\n",
3699             tag, clen,
3700             c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3701             dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3702             (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3703 }
3704
3705 static void
3706 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3707 {
3708         uint32_t sig = UGETDW(csw->dCSWSignature);
3709         uint32_t tag = UGETDW(csw->dCSWTag);
3710         uint32_t res = UGETDW(csw->dCSWDataResidue);
3711         uint8_t status = csw->bCSWStatus;
3712
3713         DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3714             "res = %d, status = 0x%02x (%s)\n",
3715             tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3716             tag, res,
3717             status, (status == CSWSTATUS_GOOD ? "good" :
3718             (status == CSWSTATUS_FAILED ? "failed" :
3719             (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3720 }
3721
3722 static void
3723 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3724 {
3725         uint8_t *c = cmd;
3726         uint8_t dir = sc->sc_transfer.dir;
3727
3728         DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3729             "(0x%02x%02x%02x%02x%02x%02x%s), "
3730             "data = %db, dir = %s\n",
3731             cmdlen,
3732             c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3733             sc->sc_transfer.data_len,
3734             (dir == DIR_IN ? "in" :
3735             (dir == DIR_OUT ? "out" :
3736             (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3737 }
3738
3739 static void
3740 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3741     uint32_t printlen)
3742 {
3743         uint32_t i, j;
3744         char s1[40];
3745         char s2[40];
3746         char s3[5];
3747
3748         s1[0] = '\0';
3749         s3[0] = '\0';
3750
3751         sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3752         for (i = 0; (i < buflen) && (i < printlen); i++) {
3753                 j = i % 16;
3754                 if (j == 0 && i != 0) {
3755                         DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3756                             s1, s2);
3757                         s2[0] = '\0';
3758                 }
3759                 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3760         }
3761         if (buflen > printlen)
3762                 sprintf(s3, " ...");
3763         DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3764             s1, s2, s3);
3765 }
3766
3767 #endif