]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/usb/storage/umass.c
MFC r240750, r241987 and r242126:
[FreeBSD/stable/9.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/module.h>
114 #include <sys/lock.h>
115 #include <sys/mutex.h>
116 #include <sys/condvar.h>
117 #include <sys/sysctl.h>
118 #include <sys/sx.h>
119 #include <sys/unistd.h>
120 #include <sys/callout.h>
121 #include <sys/malloc.h>
122 #include <sys/priv.h>
123
124 #include <dev/usb/usb.h>
125 #include <dev/usb/usbdi.h>
126 #include <dev/usb/usbdi_util.h>
127 #include "usbdevs.h"
128
129 #include <dev/usb/quirk/usb_quirk.h>
130
131 #include <cam/cam.h>
132 #include <cam/cam_ccb.h>
133 #include <cam/cam_sim.h>
134 #include <cam/cam_xpt_sim.h>
135 #include <cam/scsi/scsi_all.h>
136 #include <cam/scsi/scsi_da.h>
137
138 #include <cam/cam_periph.h>
139
140 #ifdef USB_DEBUG
141 #define DIF(m, x)                               \
142   do {                                          \
143     if (umass_debug & (m)) { x ; }              \
144   } while (0)
145
146 #define DPRINTF(sc, m, fmt, ...)                        \
147   do {                                                  \
148     if (umass_debug & (m)) {                            \
149         printf("%s:%s: " fmt,                           \
150                (sc) ? (const char *)(sc)->sc_name :     \
151                (const char *)"umassX",                  \
152                 __FUNCTION__ ,## __VA_ARGS__);          \
153     }                                                   \
154   } while (0)
155
156 #define UDMASS_GEN      0x00010000      /* general */
157 #define UDMASS_SCSI     0x00020000      /* scsi */
158 #define UDMASS_UFI      0x00040000      /* ufi command set */
159 #define UDMASS_ATAPI    0x00080000      /* 8070i command set */
160 #define UDMASS_CMD      (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
161 #define UDMASS_USB      0x00100000      /* USB general */
162 #define UDMASS_BBB      0x00200000      /* Bulk-Only transfers */
163 #define UDMASS_CBI      0x00400000      /* CBI transfers */
164 #define UDMASS_WIRE     (UDMASS_BBB|UDMASS_CBI)
165 #define UDMASS_ALL      0xffff0000      /* all of the above */
166 static int umass_debug = 0;
167
168 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
169 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
170     &umass_debug, 0, "umass debug level");
171 TUNABLE_INT("hw.usb.umass.debug", &umass_debug);
172 #else
173 #define DIF(...) do { } while (0)
174 #define DPRINTF(...) do { } while (0)
175 #endif
176
177 #define UMASS_GONE ((struct umass_softc *)1)
178
179 #define UMASS_BULK_SIZE (1 << 17)
180 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12  /* bytes */
181 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN)        /* bytes */
182
183 /* USB transfer definitions */
184
185 #define UMASS_T_BBB_RESET1      0       /* Bulk-Only */
186 #define UMASS_T_BBB_RESET2      1
187 #define UMASS_T_BBB_RESET3      2
188 #define UMASS_T_BBB_COMMAND     3
189 #define UMASS_T_BBB_DATA_READ   4
190 #define UMASS_T_BBB_DATA_RD_CS  5
191 #define UMASS_T_BBB_DATA_WRITE  6
192 #define UMASS_T_BBB_DATA_WR_CS  7
193 #define UMASS_T_BBB_STATUS      8
194 #define UMASS_T_BBB_MAX         9
195
196 #define UMASS_T_CBI_RESET1      0       /* CBI */
197 #define UMASS_T_CBI_RESET2      1
198 #define UMASS_T_CBI_RESET3      2
199 #define UMASS_T_CBI_COMMAND     3
200 #define UMASS_T_CBI_DATA_READ   4
201 #define UMASS_T_CBI_DATA_RD_CS  5
202 #define UMASS_T_CBI_DATA_WRITE  6
203 #define UMASS_T_CBI_DATA_WR_CS  7
204 #define UMASS_T_CBI_STATUS      8
205 #define UMASS_T_CBI_RESET4      9
206 #define UMASS_T_CBI_MAX        10
207
208 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
209
210 /* Generic definitions */
211
212 /* Direction for transfer */
213 #define DIR_NONE        0
214 #define DIR_IN          1
215 #define DIR_OUT         2
216
217 /* device name */
218 #define DEVNAME         "umass"
219 #define DEVNAME_SIM     "umass-sim"
220
221 /* Approximate maximum transfer speeds (assumes 33% overhead). */
222 #define UMASS_FULL_TRANSFER_SPEED       1000
223 #define UMASS_HIGH_TRANSFER_SPEED       40000
224 #define UMASS_SUPER_TRANSFER_SPEED      400000
225 #define UMASS_FLOPPY_TRANSFER_SPEED     20
226
227 #define UMASS_TIMEOUT                   5000    /* ms */
228
229 /* CAM specific definitions */
230
231 #define UMASS_SCSIID_MAX        1       /* maximum number of drives expected */
232 #define UMASS_SCSIID_HOST       UMASS_SCSIID_MAX
233
234 /* Bulk-Only features */
235
236 #define UR_BBB_RESET            0xff    /* Bulk-Only reset */
237 #define UR_BBB_GET_MAX_LUN      0xfe    /* Get maximum lun */
238
239 /* Command Block Wrapper */
240 typedef struct {
241         uDWord  dCBWSignature;
242 #define CBWSIGNATURE    0x43425355
243         uDWord  dCBWTag;
244         uDWord  dCBWDataTransferLength;
245         uByte   bCBWFlags;
246 #define CBWFLAGS_OUT    0x00
247 #define CBWFLAGS_IN     0x80
248         uByte   bCBWLUN;
249         uByte   bCDBLength;
250 #define CBWCDBLENGTH    16
251         uByte   CBWCDB[CBWCDBLENGTH];
252 } __packed umass_bbb_cbw_t;
253
254 #define UMASS_BBB_CBW_SIZE      31
255
256 /* Command Status Wrapper */
257 typedef struct {
258         uDWord  dCSWSignature;
259 #define CSWSIGNATURE    0x53425355
260 #define CSWSIGNATURE_IMAGINATION_DBX1   0x43425355
261 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
262         uDWord  dCSWTag;
263         uDWord  dCSWDataResidue;
264         uByte   bCSWStatus;
265 #define CSWSTATUS_GOOD  0x0
266 #define CSWSTATUS_FAILED        0x1
267 #define CSWSTATUS_PHASE 0x2
268 } __packed umass_bbb_csw_t;
269
270 #define UMASS_BBB_CSW_SIZE      13
271
272 /* CBI features */
273
274 #define UR_CBI_ADSC     0x00
275
276 typedef union {
277         struct {
278                 uint8_t type;
279 #define IDB_TYPE_CCI            0x00
280                 uint8_t value;
281 #define IDB_VALUE_PASS          0x00
282 #define IDB_VALUE_FAIL          0x01
283 #define IDB_VALUE_PHASE         0x02
284 #define IDB_VALUE_PERSISTENT    0x03
285 #define IDB_VALUE_STATUS_MASK   0x03
286         } __packed common;
287
288         struct {
289                 uint8_t asc;
290                 uint8_t ascq;
291         } __packed ufi;
292 } __packed umass_cbi_sbl_t;
293
294 struct umass_softc;                     /* see below */
295
296 typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
297         uint32_t residue, uint8_t status);
298
299 #define STATUS_CMD_OK           0       /* everything ok */
300 #define STATUS_CMD_UNKNOWN      1       /* will have to fetch sense */
301 #define STATUS_CMD_FAILED       2       /* transfer was ok, command failed */
302 #define STATUS_WIRE_FAILED      3       /* couldn't even get command across */
303
304 typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
305         uint8_t cmd_len);
306
307 /* Wire and command protocol */
308 #define UMASS_PROTO_BBB         0x0001  /* USB wire protocol */
309 #define UMASS_PROTO_CBI         0x0002
310 #define UMASS_PROTO_CBI_I       0x0004
311 #define UMASS_PROTO_WIRE        0x00ff  /* USB wire protocol mask */
312 #define UMASS_PROTO_SCSI        0x0100  /* command protocol */
313 #define UMASS_PROTO_ATAPI       0x0200
314 #define UMASS_PROTO_UFI         0x0400
315 #define UMASS_PROTO_RBC         0x0800
316 #define UMASS_PROTO_COMMAND     0xff00  /* command protocol mask */
317
318 /* Device specific quirks */
319 #define NO_QUIRKS               0x0000
320         /*
321          * The drive does not support Test Unit Ready. Convert to Start Unit
322          */
323 #define NO_TEST_UNIT_READY      0x0001
324         /*
325          * The drive does not reset the Unit Attention state after REQUEST
326          * SENSE has been sent. The INQUIRY command does not reset the UA
327          * either, and so CAM runs in circles trying to retrieve the initial
328          * INQUIRY data.
329          */
330 #define RS_NO_CLEAR_UA          0x0002
331         /* The drive does not support START STOP.  */
332 #define NO_START_STOP           0x0004
333         /* Don't ask for full inquiry data (255b).  */
334 #define FORCE_SHORT_INQUIRY     0x0008
335         /* Needs to be initialised the Shuttle way */
336 #define SHUTTLE_INIT            0x0010
337         /* Drive needs to be switched to alternate iface 1 */
338 #define ALT_IFACE_1             0x0020
339         /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
340 #define FLOPPY_SPEED            0x0040
341         /* The device can't count and gets the residue of transfers wrong */
342 #define IGNORE_RESIDUE          0x0080
343         /* No GetMaxLun call */
344 #define NO_GETMAXLUN            0x0100
345         /* The device uses a weird CSWSIGNATURE. */
346 #define WRONG_CSWSIG            0x0200
347         /* Device cannot handle INQUIRY so fake a generic response */
348 #define NO_INQUIRY              0x0400
349         /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
350 #define NO_INQUIRY_EVPD         0x0800
351         /* Pad all RBC requests to 12 bytes. */
352 #define RBC_PAD_TO_12           0x1000
353         /*
354          * Device reports number of sectors from READ_CAPACITY, not max
355          * sector number.
356          */
357 #define READ_CAPACITY_OFFBY1    0x2000
358         /*
359          * Device cannot handle a SCSI synchronize cache command.  Normally
360          * this quirk would be handled in the cam layer, but for IDE bridges
361          * we need to associate the quirk with the bridge and not the
362          * underlying disk device.  This is handled by faking a success
363          * result.
364          */
365 #define NO_SYNCHRONIZE_CACHE    0x4000
366
367 struct umass_softc {
368
369         struct scsi_sense cam_scsi_sense;
370         struct scsi_test_unit_ready cam_scsi_test_unit_ready;
371         struct mtx sc_mtx;
372         struct {
373                 uint8_t *data_ptr;
374                 union ccb *ccb;
375                 umass_callback_t *callback;
376
377                 uint32_t data_len;      /* bytes */
378                 uint32_t data_rem;      /* bytes */
379                 uint32_t data_timeout;  /* ms */
380                 uint32_t actlen;        /* bytes */
381
382                 uint8_t cmd_data[UMASS_MAX_CMDLEN];
383                 uint8_t cmd_len;        /* bytes */
384                 uint8_t dir;
385                 uint8_t lun;
386         }       sc_transfer;
387
388         /* Bulk specific variables for transfers in progress */
389         umass_bbb_cbw_t cbw;            /* command block wrapper */
390         umass_bbb_csw_t csw;            /* command status wrapper */
391
392         /* CBI specific variables for transfers in progress */
393         umass_cbi_sbl_t sbl;            /* status block */
394
395         device_t sc_dev;
396         struct usb_device *sc_udev;
397         struct cam_sim *sc_sim;         /* SCSI Interface Module */
398         struct usb_xfer *sc_xfer[UMASS_T_MAX];
399
400         /*
401          * The command transform function is used to convert the SCSI
402          * commands into their derivatives, like UFI, ATAPI, and friends.
403          */
404         umass_transform_t *sc_transform;
405
406         uint32_t sc_unit;
407         uint32_t sc_quirks;             /* they got it almost right */
408         uint32_t sc_proto;              /* wire and cmd protocol */
409
410         uint8_t sc_name[16];
411         uint8_t sc_iface_no;            /* interface number */
412         uint8_t sc_maxlun;              /* maximum LUN number, inclusive */
413         uint8_t sc_last_xfer_index;
414         uint8_t sc_status_try;
415 };
416
417 struct umass_probe_proto {
418         uint32_t quirks;
419         uint32_t proto;
420
421         int     error;
422 };
423
424 /* prototypes */
425
426 static device_probe_t umass_probe;
427 static device_attach_t umass_attach;
428 static device_detach_t umass_detach;
429
430 static usb_callback_t umass_tr_error;
431 static usb_callback_t umass_t_bbb_reset1_callback;
432 static usb_callback_t umass_t_bbb_reset2_callback;
433 static usb_callback_t umass_t_bbb_reset3_callback;
434 static usb_callback_t umass_t_bbb_command_callback;
435 static usb_callback_t umass_t_bbb_data_read_callback;
436 static usb_callback_t umass_t_bbb_data_rd_cs_callback;
437 static usb_callback_t umass_t_bbb_data_write_callback;
438 static usb_callback_t umass_t_bbb_data_wr_cs_callback;
439 static usb_callback_t umass_t_bbb_status_callback;
440 static usb_callback_t umass_t_cbi_reset1_callback;
441 static usb_callback_t umass_t_cbi_reset2_callback;
442 static usb_callback_t umass_t_cbi_reset3_callback;
443 static usb_callback_t umass_t_cbi_reset4_callback;
444 static usb_callback_t umass_t_cbi_command_callback;
445 static usb_callback_t umass_t_cbi_data_read_callback;
446 static usb_callback_t umass_t_cbi_data_rd_cs_callback;
447 static usb_callback_t umass_t_cbi_data_write_callback;
448 static usb_callback_t umass_t_cbi_data_wr_cs_callback;
449 static usb_callback_t umass_t_cbi_status_callback;
450
451 static void     umass_cancel_ccb(struct umass_softc *);
452 static void     umass_init_shuttle(struct umass_softc *);
453 static void     umass_reset(struct umass_softc *);
454 static void     umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
455                     uint8_t, uint8_t, usb_error_t);
456 static void     umass_command_start(struct umass_softc *, uint8_t, void *,
457                     uint32_t, uint32_t, umass_callback_t *, union ccb *);
458 static uint8_t  umass_bbb_get_max_lun(struct umass_softc *);
459 static void     umass_cbi_start_status(struct umass_softc *);
460 static void     umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
461                     uint8_t, uint8_t, usb_error_t);
462 static int      umass_cam_attach_sim(struct umass_softc *);
463 static void     umass_cam_attach(struct umass_softc *);
464 static void     umass_cam_detach_sim(struct umass_softc *);
465 static void     umass_cam_action(struct cam_sim *, union ccb *);
466 static void     umass_cam_poll(struct cam_sim *);
467 static void     umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
468                     uint8_t);
469 static void     umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
470                     uint8_t);
471 static void     umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
472                     uint8_t);
473 static uint8_t  umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
474 static uint8_t  umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
475 static uint8_t  umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
476 static uint8_t  umass_atapi_transform(struct umass_softc *, uint8_t *,
477                     uint8_t);
478 static uint8_t  umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
479 static uint8_t  umass_std_transform(struct umass_softc *, union ccb *, uint8_t
480                     *, uint8_t);
481
482 #ifdef USB_DEBUG
483 static void     umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
484 static void     umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
485 static void     umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
486 static void     umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
487                     uint32_t);
488 #endif
489
490 static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
491
492         [UMASS_T_BBB_RESET1] = {
493                 .type = UE_CONTROL,
494                 .endpoint = 0x00,       /* Control pipe */
495                 .direction = UE_DIR_ANY,
496                 .bufsize = sizeof(struct usb_device_request),
497                 .callback = &umass_t_bbb_reset1_callback,
498                 .timeout = 5000,        /* 5 seconds */
499                 .interval = 500,        /* 500 milliseconds */
500         },
501
502         [UMASS_T_BBB_RESET2] = {
503                 .type = UE_CONTROL,
504                 .endpoint = 0x00,       /* Control pipe */
505                 .direction = UE_DIR_ANY,
506                 .bufsize = sizeof(struct usb_device_request),
507                 .callback = &umass_t_bbb_reset2_callback,
508                 .timeout = 5000,        /* 5 seconds */
509                 .interval = 50, /* 50 milliseconds */
510         },
511
512         [UMASS_T_BBB_RESET3] = {
513                 .type = UE_CONTROL,
514                 .endpoint = 0x00,       /* Control pipe */
515                 .direction = UE_DIR_ANY,
516                 .bufsize = sizeof(struct usb_device_request),
517                 .callback = &umass_t_bbb_reset3_callback,
518                 .timeout = 5000,        /* 5 seconds */
519                 .interval = 50, /* 50 milliseconds */
520         },
521
522         [UMASS_T_BBB_COMMAND] = {
523                 .type = UE_BULK,
524                 .endpoint = UE_ADDR_ANY,
525                 .direction = UE_DIR_OUT,
526                 .bufsize = sizeof(umass_bbb_cbw_t),
527                 .callback = &umass_t_bbb_command_callback,
528                 .timeout = 5000,        /* 5 seconds */
529         },
530
531         [UMASS_T_BBB_DATA_READ] = {
532                 .type = UE_BULK,
533                 .endpoint = UE_ADDR_ANY,
534                 .direction = UE_DIR_IN,
535                 .bufsize = UMASS_BULK_SIZE,
536                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
537                 .callback = &umass_t_bbb_data_read_callback,
538                 .timeout = 0,   /* overwritten later */
539         },
540
541         [UMASS_T_BBB_DATA_RD_CS] = {
542                 .type = UE_CONTROL,
543                 .endpoint = 0x00,       /* Control pipe */
544                 .direction = UE_DIR_ANY,
545                 .bufsize = sizeof(struct usb_device_request),
546                 .callback = &umass_t_bbb_data_rd_cs_callback,
547                 .timeout = 5000,        /* 5 seconds */
548         },
549
550         [UMASS_T_BBB_DATA_WRITE] = {
551                 .type = UE_BULK,
552                 .endpoint = UE_ADDR_ANY,
553                 .direction = UE_DIR_OUT,
554                 .bufsize = UMASS_BULK_SIZE,
555                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
556                 .callback = &umass_t_bbb_data_write_callback,
557                 .timeout = 0,   /* overwritten later */
558         },
559
560         [UMASS_T_BBB_DATA_WR_CS] = {
561                 .type = UE_CONTROL,
562                 .endpoint = 0x00,       /* Control pipe */
563                 .direction = UE_DIR_ANY,
564                 .bufsize = sizeof(struct usb_device_request),
565                 .callback = &umass_t_bbb_data_wr_cs_callback,
566                 .timeout = 5000,        /* 5 seconds */
567         },
568
569         [UMASS_T_BBB_STATUS] = {
570                 .type = UE_BULK,
571                 .endpoint = UE_ADDR_ANY,
572                 .direction = UE_DIR_IN,
573                 .bufsize = sizeof(umass_bbb_csw_t),
574                 .flags = {.short_xfer_ok = 1,},
575                 .callback = &umass_t_bbb_status_callback,
576                 .timeout = 5000,        /* ms */
577         },
578 };
579
580 static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
581
582         [UMASS_T_CBI_RESET1] = {
583                 .type = UE_CONTROL,
584                 .endpoint = 0x00,       /* Control pipe */
585                 .direction = UE_DIR_ANY,
586                 .bufsize = (sizeof(struct usb_device_request) +
587                     UMASS_CBI_DIAGNOSTIC_CMDLEN),
588                 .callback = &umass_t_cbi_reset1_callback,
589                 .timeout = 5000,        /* 5 seconds */
590                 .interval = 500,        /* 500 milliseconds */
591         },
592
593         [UMASS_T_CBI_RESET2] = {
594                 .type = UE_CONTROL,
595                 .endpoint = 0x00,       /* Control pipe */
596                 .direction = UE_DIR_ANY,
597                 .bufsize = sizeof(struct usb_device_request),
598                 .callback = &umass_t_cbi_reset2_callback,
599                 .timeout = 5000,        /* 5 seconds */
600                 .interval = 50, /* 50 milliseconds */
601         },
602
603         [UMASS_T_CBI_RESET3] = {
604                 .type = UE_CONTROL,
605                 .endpoint = 0x00,       /* Control pipe */
606                 .direction = UE_DIR_ANY,
607                 .bufsize = sizeof(struct usb_device_request),
608                 .callback = &umass_t_cbi_reset3_callback,
609                 .timeout = 5000,        /* 5 seconds */
610                 .interval = 50, /* 50 milliseconds */
611         },
612
613         [UMASS_T_CBI_COMMAND] = {
614                 .type = UE_CONTROL,
615                 .endpoint = 0x00,       /* Control pipe */
616                 .direction = UE_DIR_ANY,
617                 .bufsize = (sizeof(struct usb_device_request) +
618                     UMASS_MAX_CMDLEN),
619                 .callback = &umass_t_cbi_command_callback,
620                 .timeout = 5000,        /* 5 seconds */
621         },
622
623         [UMASS_T_CBI_DATA_READ] = {
624                 .type = UE_BULK,
625                 .endpoint = UE_ADDR_ANY,
626                 .direction = UE_DIR_IN,
627                 .bufsize = UMASS_BULK_SIZE,
628                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
629                 .callback = &umass_t_cbi_data_read_callback,
630                 .timeout = 0,   /* overwritten later */
631         },
632
633         [UMASS_T_CBI_DATA_RD_CS] = {
634                 .type = UE_CONTROL,
635                 .endpoint = 0x00,       /* Control pipe */
636                 .direction = UE_DIR_ANY,
637                 .bufsize = sizeof(struct usb_device_request),
638                 .callback = &umass_t_cbi_data_rd_cs_callback,
639                 .timeout = 5000,        /* 5 seconds */
640         },
641
642         [UMASS_T_CBI_DATA_WRITE] = {
643                 .type = UE_BULK,
644                 .endpoint = UE_ADDR_ANY,
645                 .direction = UE_DIR_OUT,
646                 .bufsize = UMASS_BULK_SIZE,
647                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
648                 .callback = &umass_t_cbi_data_write_callback,
649                 .timeout = 0,   /* overwritten later */
650         },
651
652         [UMASS_T_CBI_DATA_WR_CS] = {
653                 .type = UE_CONTROL,
654                 .endpoint = 0x00,       /* Control pipe */
655                 .direction = UE_DIR_ANY,
656                 .bufsize = sizeof(struct usb_device_request),
657                 .callback = &umass_t_cbi_data_wr_cs_callback,
658                 .timeout = 5000,        /* 5 seconds */
659         },
660
661         [UMASS_T_CBI_STATUS] = {
662                 .type = UE_INTERRUPT,
663                 .endpoint = UE_ADDR_ANY,
664                 .direction = UE_DIR_IN,
665                 .flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,},
666                 .bufsize = sizeof(umass_cbi_sbl_t),
667                 .callback = &umass_t_cbi_status_callback,
668                 .timeout = 5000,        /* ms */
669         },
670
671         [UMASS_T_CBI_RESET4] = {
672                 .type = UE_CONTROL,
673                 .endpoint = 0x00,       /* Control pipe */
674                 .direction = UE_DIR_ANY,
675                 .bufsize = sizeof(struct usb_device_request),
676                 .callback = &umass_t_cbi_reset4_callback,
677                 .timeout = 5000,        /* ms */
678         },
679 };
680
681 /* If device cannot return valid inquiry data, fake it */
682 static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
683         0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
684          /* additional_length */ 31, 0, 0, 0
685 };
686
687 #define UFI_COMMAND_LENGTH      12      /* UFI commands are always 12 bytes */
688 #define ATAPI_COMMAND_LENGTH    12      /* ATAPI commands are always 12 bytes */
689
690 static devclass_t umass_devclass;
691
692 static device_method_t umass_methods[] = {
693         /* Device interface */
694         DEVMETHOD(device_probe, umass_probe),
695         DEVMETHOD(device_attach, umass_attach),
696         DEVMETHOD(device_detach, umass_detach),
697         {0, 0}
698 };
699
700 static driver_t umass_driver = {
701         .name = "umass",
702         .methods = umass_methods,
703         .size = sizeof(struct umass_softc),
704 };
705
706 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
707 MODULE_DEPEND(umass, usb, 1, 1, 1);
708 MODULE_DEPEND(umass, cam, 1, 1, 1);
709 MODULE_VERSION(umass, 1);
710
711 /*
712  * USB device probe/attach/detach
713  */
714
715 static const STRUCT_USB_HOST_ID __used umass_devs[] = {
716         /* generic mass storage class */
717         {USB_IFACE_CLASS(UICLASS_MASS),},
718 };
719
720 static uint16_t
721 umass_get_proto(struct usb_interface *iface)
722 {
723         struct usb_interface_descriptor *id;
724         uint16_t retval;
725
726         retval = 0;
727
728         /* Check for a standards compliant device */
729         id = usbd_get_interface_descriptor(iface);
730         if ((id == NULL) ||
731             (id->bInterfaceClass != UICLASS_MASS)) {
732                 goto done;
733         }
734         switch (id->bInterfaceSubClass) {
735         case UISUBCLASS_SCSI:
736                 retval |= UMASS_PROTO_SCSI;
737                 break;
738         case UISUBCLASS_UFI:
739                 retval |= UMASS_PROTO_UFI;
740                 break;
741         case UISUBCLASS_RBC:
742                 retval |= UMASS_PROTO_RBC;
743                 break;
744         case UISUBCLASS_SFF8020I:
745         case UISUBCLASS_SFF8070I:
746                 retval |= UMASS_PROTO_ATAPI;
747                 break;
748         default:
749                 goto done;
750         }
751
752         switch (id->bInterfaceProtocol) {
753         case UIPROTO_MASS_CBI:
754                 retval |= UMASS_PROTO_CBI;
755                 break;
756         case UIPROTO_MASS_CBI_I:
757                 retval |= UMASS_PROTO_CBI_I;
758                 break;
759         case UIPROTO_MASS_BBB_OLD:
760         case UIPROTO_MASS_BBB:
761                 retval |= UMASS_PROTO_BBB;
762                 break;
763         default:
764                 goto done;
765         }
766 done:
767         return (retval);
768 }
769
770 /*
771  * Match the device we are seeing with the devices supported.
772  */
773 static struct umass_probe_proto
774 umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
775 {
776         struct umass_probe_proto ret;
777         uint32_t quirks = NO_QUIRKS;
778         uint32_t proto = umass_get_proto(uaa->iface);
779
780         memset(&ret, 0, sizeof(ret));
781         ret.error = BUS_PROBE_GENERIC;
782
783         /* Search for protocol enforcement */
784
785         if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) {
786                 proto &= ~UMASS_PROTO_WIRE;
787                 proto |= UMASS_PROTO_BBB;
788         } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) {
789                 proto &= ~UMASS_PROTO_WIRE;
790                 proto |= UMASS_PROTO_CBI;
791         } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) {
792                 proto &= ~UMASS_PROTO_WIRE;
793                 proto |= UMASS_PROTO_CBI_I;
794         }
795
796         if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) {
797                 proto &= ~UMASS_PROTO_COMMAND;
798                 proto |= UMASS_PROTO_SCSI;
799         } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) {
800                 proto &= ~UMASS_PROTO_COMMAND;
801                 proto |= UMASS_PROTO_ATAPI;
802         } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) {
803                 proto &= ~UMASS_PROTO_COMMAND;
804                 proto |= UMASS_PROTO_UFI;
805         } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) {
806                 proto &= ~UMASS_PROTO_COMMAND;
807                 proto |= UMASS_PROTO_RBC;
808         }
809
810         /* Check if the protocol is invalid */
811
812         if ((proto & UMASS_PROTO_COMMAND) == 0) {
813                 ret.error = ENXIO;
814                 goto done;
815         }
816
817         if ((proto & UMASS_PROTO_WIRE) == 0) {
818                 ret.error = ENXIO;
819                 goto done;
820         }
821
822         /* Search for quirks */
823
824         if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY))
825                 quirks |= NO_TEST_UNIT_READY;
826         if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA))
827                 quirks |= RS_NO_CLEAR_UA;
828         if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP))
829                 quirks |= NO_START_STOP;
830         if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN))
831                 quirks |= NO_GETMAXLUN;
832         if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY))
833                 quirks |= NO_INQUIRY;
834         if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD))
835                 quirks |= NO_INQUIRY_EVPD;
836         if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE))
837                 quirks |= NO_SYNCHRONIZE_CACHE;
838         if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT))
839                 quirks |= SHUTTLE_INIT;
840         if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1))
841                 quirks |= ALT_IFACE_1;
842         if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED))
843                 quirks |= FLOPPY_SPEED;
844         if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE))
845                 quirks |= IGNORE_RESIDUE;
846         if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG))
847                 quirks |= WRONG_CSWSIG;
848         if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12))
849                 quirks |= RBC_PAD_TO_12;
850         if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1))
851                 quirks |= READ_CAPACITY_OFFBY1;
852         if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ))
853                 quirks |= FORCE_SHORT_INQUIRY;
854
855 done:
856         ret.quirks = quirks;
857         ret.proto = proto;
858         return (ret);
859 }
860
861 static int
862 umass_probe(device_t dev)
863 {
864         struct usb_attach_arg *uaa = device_get_ivars(dev);
865         struct umass_probe_proto temp;
866
867         if (uaa->usb_mode != USB_MODE_HOST) {
868                 return (ENXIO);
869         }
870         temp = umass_probe_proto(dev, uaa);
871
872         return (temp.error);
873 }
874
875 static int
876 umass_attach(device_t dev)
877 {
878         struct umass_softc *sc = device_get_softc(dev);
879         struct usb_attach_arg *uaa = device_get_ivars(dev);
880         struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
881         struct usb_interface_descriptor *id;
882         int32_t err;
883
884         /*
885          * NOTE: the softc struct is cleared in device_set_driver.
886          * We can safely call umass_detach without specifically
887          * initializing the struct.
888          */
889
890         sc->sc_dev = dev;
891         sc->sc_udev = uaa->device;
892         sc->sc_proto = temp.proto;
893         sc->sc_quirks = temp.quirks;
894         sc->sc_unit = device_get_unit(dev);
895
896         snprintf(sc->sc_name, sizeof(sc->sc_name),
897             "%s", device_get_nameunit(dev));
898
899         device_set_usb_desc(dev);
900
901         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), 
902             NULL, MTX_DEF | MTX_RECURSE);
903
904         /* get interface index */
905
906         id = usbd_get_interface_descriptor(uaa->iface);
907         if (id == NULL) {
908                 device_printf(dev, "failed to get "
909                     "interface number\n");
910                 goto detach;
911         }
912         sc->sc_iface_no = id->bInterfaceNumber;
913
914 #ifdef USB_DEBUG
915         device_printf(dev, " ");
916
917         switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
918         case UMASS_PROTO_SCSI:
919                 printf("SCSI");
920                 break;
921         case UMASS_PROTO_ATAPI:
922                 printf("8070i (ATAPI)");
923                 break;
924         case UMASS_PROTO_UFI:
925                 printf("UFI");
926                 break;
927         case UMASS_PROTO_RBC:
928                 printf("RBC");
929                 break;
930         default:
931                 printf("(unknown 0x%02x)",
932                     sc->sc_proto & UMASS_PROTO_COMMAND);
933                 break;
934         }
935
936         printf(" over ");
937
938         switch (sc->sc_proto & UMASS_PROTO_WIRE) {
939         case UMASS_PROTO_BBB:
940                 printf("Bulk-Only");
941                 break;
942         case UMASS_PROTO_CBI:           /* uses Comand/Bulk pipes */
943                 printf("CBI");
944                 break;
945         case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
946                 printf("CBI with CCI");
947                 break;
948         default:
949                 printf("(unknown 0x%02x)",
950                     sc->sc_proto & UMASS_PROTO_WIRE);
951         }
952
953         printf("; quirks = 0x%04x\n", sc->sc_quirks);
954 #endif
955
956         if (sc->sc_quirks & ALT_IFACE_1) {
957                 err = usbd_set_alt_interface_index
958                     (uaa->device, uaa->info.bIfaceIndex, 1);
959
960                 if (err) {
961                         DPRINTF(sc, UDMASS_USB, "could not switch to "
962                             "Alt Interface 1\n");
963                         goto detach;
964                 }
965         }
966         /* allocate all required USB transfers */
967
968         if (sc->sc_proto & UMASS_PROTO_BBB) {
969
970                 err = usbd_transfer_setup(uaa->device,
971                     &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
972                     UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
973
974                 /* skip reset first time */
975                 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
976
977         } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
978
979                 err = usbd_transfer_setup(uaa->device,
980                     &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
981                     UMASS_T_CBI_MAX, sc, &sc->sc_mtx);
982
983                 /* skip reset first time */
984                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
985
986         } else {
987                 err = USB_ERR_INVAL;
988         }
989
990         if (err) {
991                 device_printf(dev, "could not setup required "
992                     "transfers, %s\n", usbd_errstr(err));
993                 goto detach;
994         }
995         sc->sc_transform =
996             (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
997             (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
998             (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
999             (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1000             &umass_no_transform;
1001
1002         /* from here onwards the device can be used. */
1003
1004         if (sc->sc_quirks & SHUTTLE_INIT) {
1005                 umass_init_shuttle(sc);
1006         }
1007         /* get the maximum LUN supported by the device */
1008
1009         if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1010             !(sc->sc_quirks & NO_GETMAXLUN))
1011                 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1012         else
1013                 sc->sc_maxlun = 0;
1014
1015         /* Prepare the SCSI command block */
1016         sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1017         sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1018
1019         /* register the SIM */
1020         err = umass_cam_attach_sim(sc);
1021         if (err) {
1022                 goto detach;
1023         }
1024         /* scan the SIM */
1025         umass_cam_attach(sc);
1026
1027         DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1028
1029         return (0);                     /* success */
1030
1031 detach:
1032         umass_detach(dev);
1033         return (ENXIO);                 /* failure */
1034 }
1035
1036 static int
1037 umass_detach(device_t dev)
1038 {
1039         struct umass_softc *sc = device_get_softc(dev);
1040
1041         DPRINTF(sc, UDMASS_USB, "\n");
1042
1043         /* teardown our statemachine */
1044
1045         usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1046
1047         mtx_lock(&sc->sc_mtx);
1048
1049         /* cancel any leftover CCB's */
1050
1051         umass_cancel_ccb(sc);
1052
1053         umass_cam_detach_sim(sc);
1054
1055         mtx_unlock(&sc->sc_mtx);
1056
1057         mtx_destroy(&sc->sc_mtx);
1058
1059         return (0);                     /* success */
1060 }
1061
1062 static void
1063 umass_init_shuttle(struct umass_softc *sc)
1064 {
1065         struct usb_device_request req;
1066         usb_error_t err;
1067         uint8_t status[2] = {0, 0};
1068
1069         /*
1070          * The Linux driver does this, but no one can tell us what the
1071          * command does.
1072          */
1073         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1074         req.bRequest = 1;               /* XXX unknown command */
1075         USETW(req.wValue, 0);
1076         req.wIndex[0] = sc->sc_iface_no;
1077         req.wIndex[1] = 0;
1078         USETW(req.wLength, sizeof(status));
1079         err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1080
1081         DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1082             status[0], status[1]);
1083 }
1084
1085 /*
1086  * Generic functions to handle transfers
1087  */
1088
1089 static void
1090 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1091 {
1092         DPRINTF(sc, UDMASS_GEN, "transfer index = "
1093             "%d\n", xfer_index);
1094
1095         if (sc->sc_xfer[xfer_index]) {
1096                 sc->sc_last_xfer_index = xfer_index;
1097                 usbd_transfer_start(sc->sc_xfer[xfer_index]);
1098         } else {
1099                 umass_cancel_ccb(sc);
1100         }
1101 }
1102
1103 static void
1104 umass_reset(struct umass_softc *sc)
1105 {
1106         DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1107
1108         /*
1109          * stop the last transfer, if not already stopped:
1110          */
1111         usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1112         umass_transfer_start(sc, 0);
1113 }
1114
1115 static void
1116 umass_cancel_ccb(struct umass_softc *sc)
1117 {
1118         union ccb *ccb;
1119
1120         mtx_assert(&sc->sc_mtx, MA_OWNED);
1121
1122         ccb = sc->sc_transfer.ccb;
1123         sc->sc_transfer.ccb = NULL;
1124         sc->sc_last_xfer_index = 0;
1125
1126         if (ccb) {
1127                 (sc->sc_transfer.callback)
1128                     (sc, ccb, (sc->sc_transfer.data_len -
1129                     sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1130         }
1131 }
1132
1133 static void
1134 umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1135 {
1136         struct umass_softc *sc = usbd_xfer_softc(xfer);
1137
1138         if (error != USB_ERR_CANCELLED) {
1139
1140                 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1141                     "reset\n", usbd_errstr(error));
1142         }
1143         umass_cancel_ccb(sc);
1144 }
1145
1146 /*
1147  * BBB protocol specific functions
1148  */
1149
1150 static void
1151 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1152 {
1153         struct umass_softc *sc = usbd_xfer_softc(xfer);
1154         struct usb_device_request req;
1155         struct usb_page_cache *pc;
1156
1157         switch (USB_GET_STATE(xfer)) {
1158         case USB_ST_TRANSFERRED:
1159                 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1160                 return;
1161
1162         case USB_ST_SETUP:
1163                 /*
1164                  * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1165                  *
1166                  * For Reset Recovery the host shall issue in the following order:
1167                  * a) a Bulk-Only Mass Storage Reset
1168                  * b) a Clear Feature HALT to the Bulk-In endpoint
1169                  * c) a Clear Feature HALT to the Bulk-Out endpoint
1170                  *
1171                  * This is done in 3 steps, using 3 transfers:
1172                  * UMASS_T_BBB_RESET1
1173                  * UMASS_T_BBB_RESET2
1174                  * UMASS_T_BBB_RESET3
1175                  */
1176
1177                 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1178
1179                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1180                 req.bRequest = UR_BBB_RESET;    /* bulk only reset */
1181                 USETW(req.wValue, 0);
1182                 req.wIndex[0] = sc->sc_iface_no;
1183                 req.wIndex[1] = 0;
1184                 USETW(req.wLength, 0);
1185
1186                 pc = usbd_xfer_get_frame(xfer, 0);
1187                 usbd_copy_in(pc, 0, &req, sizeof(req));
1188
1189                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1190                 usbd_xfer_set_frames(xfer, 1);
1191                 usbd_transfer_submit(xfer);
1192                 return;
1193
1194         default:                        /* Error */
1195                 umass_tr_error(xfer, error);
1196                 return;
1197         }
1198 }
1199
1200 static void
1201 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1202 {
1203         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1204             UMASS_T_BBB_DATA_READ, error);
1205 }
1206
1207 static void
1208 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1209 {
1210         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1211             UMASS_T_BBB_DATA_WRITE, error);
1212 }
1213
1214 static void
1215 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1216     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1217 {
1218         struct umass_softc *sc = usbd_xfer_softc(xfer);
1219
1220         switch (USB_GET_STATE(xfer)) {
1221         case USB_ST_TRANSFERRED:
1222 tr_transferred:
1223                 umass_transfer_start(sc, next_xfer);
1224                 return;
1225
1226         case USB_ST_SETUP:
1227                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1228                         goto tr_transferred;
1229                 }
1230                 return;
1231
1232         default:                        /* Error */
1233                 umass_tr_error(xfer, error);
1234                 return;
1235         }
1236 }
1237
1238 static void
1239 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1240 {
1241         struct umass_softc *sc = usbd_xfer_softc(xfer);
1242         union ccb *ccb = sc->sc_transfer.ccb;
1243         struct usb_page_cache *pc;
1244         uint32_t tag;
1245
1246         switch (USB_GET_STATE(xfer)) {
1247         case USB_ST_TRANSFERRED:
1248                 umass_transfer_start
1249                     (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1250                     (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1251                     UMASS_T_BBB_STATUS));
1252                 return;
1253
1254         case USB_ST_SETUP:
1255
1256                 sc->sc_status_try = 0;
1257
1258                 if (ccb) {
1259
1260                         /*
1261                          * the initial value is not important,
1262                          * as long as the values are unique:
1263                          */
1264                         tag = UGETDW(sc->cbw.dCBWTag) + 1;
1265
1266                         USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1267                         USETDW(sc->cbw.dCBWTag, tag);
1268
1269                         /*
1270                          * dCBWDataTransferLength:
1271                          *   This field indicates the number of bytes of data that the host
1272                          *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1273                          *   the Direction bit) during the execution of this command. If this
1274                          *   field is set to 0, the device will expect that no data will be
1275                          *   transferred IN or OUT during this command, regardless of the value
1276                          *   of the Direction bit defined in dCBWFlags.
1277                          */
1278                         USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1279
1280                         /*
1281                          * dCBWFlags:
1282                          *   The bits of the Flags field are defined as follows:
1283                          *     Bits 0-6  reserved
1284                          *     Bit  7    Direction - this bit shall be ignored if the
1285                          *                           dCBWDataTransferLength field is zero.
1286                          *               0 = data Out from host to device
1287                          *               1 = data In from device to host
1288                          */
1289                         sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1290                             CBWFLAGS_IN : CBWFLAGS_OUT);
1291                         sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1292
1293                         if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1294                                 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1295                                 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1296                         }
1297                         sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1298
1299                         memcpy(sc->cbw.CBWCDB, sc->sc_transfer.cmd_data,
1300                             sc->sc_transfer.cmd_len);
1301
1302                         memset(sc->sc_transfer.cmd_data +
1303                             sc->sc_transfer.cmd_len, 0,
1304                             sizeof(sc->cbw.CBWCDB) -
1305                             sc->sc_transfer.cmd_len);
1306
1307                         DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1308
1309                         pc = usbd_xfer_get_frame(xfer, 0);
1310                         usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1311                         usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1312
1313                         usbd_transfer_submit(xfer);
1314                 }
1315                 return;
1316
1317         default:                        /* Error */
1318                 umass_tr_error(xfer, error);
1319                 return;
1320         }
1321 }
1322
1323 static void
1324 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1325 {
1326         struct umass_softc *sc = usbd_xfer_softc(xfer);
1327         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1328         int actlen, sumlen;
1329
1330         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1331
1332         switch (USB_GET_STATE(xfer)) {
1333         case USB_ST_TRANSFERRED:
1334                 sc->sc_transfer.data_rem -= actlen;
1335                 sc->sc_transfer.data_ptr += actlen;
1336                 sc->sc_transfer.actlen += actlen;
1337
1338                 if (actlen < sumlen) {
1339                         /* short transfer */
1340                         sc->sc_transfer.data_rem = 0;
1341                 }
1342         case USB_ST_SETUP:
1343                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1344                     max_bulk, sc->sc_transfer.data_rem);
1345
1346                 if (sc->sc_transfer.data_rem == 0) {
1347                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1348                         return;
1349                 }
1350                 if (max_bulk > sc->sc_transfer.data_rem) {
1351                         max_bulk = sc->sc_transfer.data_rem;
1352                 }
1353                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1354
1355                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1356                     max_bulk);
1357
1358                 usbd_transfer_submit(xfer);
1359                 return;
1360
1361         default:                        /* Error */
1362                 if (error == USB_ERR_CANCELLED) {
1363                         umass_tr_error(xfer, error);
1364                 } else {
1365                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1366                 }
1367                 return;
1368         }
1369 }
1370
1371 static void
1372 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1373 {
1374         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1375             UMASS_T_BBB_DATA_READ, error);
1376 }
1377
1378 static void
1379 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1380 {
1381         struct umass_softc *sc = usbd_xfer_softc(xfer);
1382         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1383         int actlen, sumlen;
1384
1385         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1386
1387         switch (USB_GET_STATE(xfer)) {
1388         case USB_ST_TRANSFERRED:
1389                 sc->sc_transfer.data_rem -= actlen;
1390                 sc->sc_transfer.data_ptr += actlen;
1391                 sc->sc_transfer.actlen += actlen;
1392
1393                 if (actlen < sumlen) {
1394                         /* short transfer */
1395                         sc->sc_transfer.data_rem = 0;
1396                 }
1397         case USB_ST_SETUP:
1398                 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1399                     max_bulk, sc->sc_transfer.data_rem);
1400
1401                 if (sc->sc_transfer.data_rem == 0) {
1402                         umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1403                         return;
1404                 }
1405                 if (max_bulk > sc->sc_transfer.data_rem) {
1406                         max_bulk = sc->sc_transfer.data_rem;
1407                 }
1408                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1409
1410                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1411                     max_bulk);
1412
1413                 usbd_transfer_submit(xfer);
1414                 return;
1415
1416         default:                        /* Error */
1417                 if (error == USB_ERR_CANCELLED) {
1418                         umass_tr_error(xfer, error);
1419                 } else {
1420                         umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1421                 }
1422                 return;
1423         }
1424 }
1425
1426 static void
1427 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1428 {
1429         umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1430             UMASS_T_BBB_DATA_WRITE, error);
1431 }
1432
1433 static void
1434 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
1435 {
1436         struct umass_softc *sc = usbd_xfer_softc(xfer);
1437         union ccb *ccb = sc->sc_transfer.ccb;
1438         struct usb_page_cache *pc;
1439         uint32_t residue;
1440         int actlen;
1441
1442         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1443
1444         switch (USB_GET_STATE(xfer)) {
1445         case USB_ST_TRANSFERRED:
1446
1447                 /*
1448                  * Do a full reset if there is something wrong with the CSW:
1449                  */
1450                 sc->sc_status_try = 1;
1451
1452                 /* Zero missing parts of the CSW: */
1453
1454                 if (actlen < (int)sizeof(sc->csw))
1455                         memset(&sc->csw, 0, sizeof(sc->csw));
1456
1457                 pc = usbd_xfer_get_frame(xfer, 0);
1458                 usbd_copy_out(pc, 0, &sc->csw, actlen);
1459
1460                 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1461
1462                 residue = UGETDW(sc->csw.dCSWDataResidue);
1463
1464                 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
1465                         residue = (sc->sc_transfer.data_len -
1466                             sc->sc_transfer.actlen);
1467                 }
1468                 if (residue > sc->sc_transfer.data_len) {
1469                         DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
1470                             "to %d bytes\n", residue, sc->sc_transfer.data_len);
1471                         residue = sc->sc_transfer.data_len;
1472                 }
1473                 /* translate weird command-status signatures: */
1474                 if (sc->sc_quirks & WRONG_CSWSIG) {
1475
1476                         uint32_t temp = UGETDW(sc->csw.dCSWSignature);
1477
1478                         if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
1479                             (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
1480                                 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1481                         }
1482                 }
1483                 /* check CSW and handle eventual error */
1484                 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1485                         DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
1486                             UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
1487                         /*
1488                          * Invalid CSW: Wrong signature or wrong tag might
1489                          * indicate that we lost synchronization. Reset the
1490                          * device.
1491                          */
1492                         goto tr_error;
1493                 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
1494                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
1495                             "0x%08x\n", UGETDW(sc->csw.dCSWTag),
1496                             UGETDW(sc->cbw.dCBWTag));
1497                         goto tr_error;
1498                 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1499                         DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
1500                             sc->csw.bCSWStatus, CSWSTATUS_PHASE);
1501                         goto tr_error;
1502                 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1503                         DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
1504                             "%d\n", residue);
1505                         goto tr_error;
1506                 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
1507                         DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
1508                             sc->sc_transfer.actlen, sc->sc_transfer.data_len);
1509                         goto tr_error;
1510                 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1511                         DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
1512                             "%d\n", residue);
1513
1514                         sc->sc_transfer.ccb = NULL;
1515
1516                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1517
1518                         (sc->sc_transfer.callback)
1519                             (sc, ccb, residue, STATUS_CMD_FAILED);
1520                 } else {
1521                         sc->sc_transfer.ccb = NULL;
1522
1523                         sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1524
1525                         (sc->sc_transfer.callback)
1526                             (sc, ccb, residue, STATUS_CMD_OK);
1527                 }
1528                 return;
1529
1530         case USB_ST_SETUP:
1531                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1532                 usbd_transfer_submit(xfer);
1533                 return;
1534
1535         default:
1536 tr_error:
1537                 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
1538                     usbd_errstr(error), sc->sc_status_try);
1539
1540                 if ((error == USB_ERR_CANCELLED) ||
1541                     (sc->sc_status_try)) {
1542                         umass_tr_error(xfer, error);
1543                 } else {
1544                         sc->sc_status_try = 1;
1545                         umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1546                 }
1547                 return;
1548         }
1549 }
1550
1551 static void
1552 umass_command_start(struct umass_softc *sc, uint8_t dir,
1553     void *data_ptr, uint32_t data_len,
1554     uint32_t data_timeout, umass_callback_t *callback,
1555     union ccb *ccb)
1556 {
1557         sc->sc_transfer.lun = ccb->ccb_h.target_lun;
1558
1559         /*
1560          * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1561          * "sc->sc_transfer.cmd_len" has been properly
1562          * initialized.
1563          */
1564
1565         sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
1566         sc->sc_transfer.data_ptr = data_ptr;
1567         sc->sc_transfer.data_len = data_len;
1568         sc->sc_transfer.data_rem = data_len;
1569         sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
1570
1571         sc->sc_transfer.actlen = 0;
1572         sc->sc_transfer.callback = callback;
1573         sc->sc_transfer.ccb = ccb;
1574
1575         if (sc->sc_xfer[sc->sc_last_xfer_index]) {
1576                 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
1577         } else {
1578                 umass_cancel_ccb(sc);
1579         }
1580 }
1581
1582 static uint8_t
1583 umass_bbb_get_max_lun(struct umass_softc *sc)
1584 {
1585         struct usb_device_request req;
1586         usb_error_t err;
1587         uint8_t buf = 0;
1588
1589         /* The Get Max Lun command is a class-specific request. */
1590         req.bmRequestType = UT_READ_CLASS_INTERFACE;
1591         req.bRequest = UR_BBB_GET_MAX_LUN;
1592         USETW(req.wValue, 0);
1593         req.wIndex[0] = sc->sc_iface_no;
1594         req.wIndex[1] = 0;
1595         USETW(req.wLength, 1);
1596
1597         err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
1598         if (err) {
1599                 buf = 0;
1600
1601                 /* Device doesn't support Get Max Lun request. */
1602                 printf("%s: Get Max Lun not supported (%s)\n",
1603                     sc->sc_name, usbd_errstr(err));
1604         }
1605         return (buf);
1606 }
1607
1608 /*
1609  * Command/Bulk/Interrupt (CBI) specific functions
1610  */
1611
1612 static void
1613 umass_cbi_start_status(struct umass_softc *sc)
1614 {
1615         if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
1616                 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
1617         } else {
1618                 union ccb *ccb = sc->sc_transfer.ccb;
1619
1620                 sc->sc_transfer.ccb = NULL;
1621
1622                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1623
1624                 (sc->sc_transfer.callback)
1625                     (sc, ccb, (sc->sc_transfer.data_len -
1626                     sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
1627         }
1628 }
1629
1630 static void
1631 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1632 {
1633         struct umass_softc *sc = usbd_xfer_softc(xfer);
1634         struct usb_device_request req;
1635         struct usb_page_cache *pc;
1636         uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
1637
1638         uint8_t i;
1639
1640         switch (USB_GET_STATE(xfer)) {
1641         case USB_ST_TRANSFERRED:
1642                 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1643                 break;
1644
1645         case USB_ST_SETUP:
1646                 /*
1647                  * Command Block Reset Protocol
1648                  *
1649                  * First send a reset request to the device. Then clear
1650                  * any possibly stalled bulk endpoints.
1651                  *
1652                  * This is done in 3 steps, using 3 transfers:
1653                  * UMASS_T_CBI_RESET1
1654                  * UMASS_T_CBI_RESET2
1655                  * UMASS_T_CBI_RESET3
1656                  * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1657                  */
1658
1659                 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
1660
1661                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1662                 req.bRequest = UR_CBI_ADSC;
1663                 USETW(req.wValue, 0);
1664                 req.wIndex[0] = sc->sc_iface_no;
1665                 req.wIndex[1] = 0;
1666                 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
1667
1668                 /*
1669                  * The 0x1d code is the SEND DIAGNOSTIC command. To
1670                  * distinguish between the two, the last 10 bytes of the CBL
1671                  * is filled with 0xff (section 2.2 of the CBI
1672                  * specification)
1673                  */
1674                 buf[0] = 0x1d;          /* Command Block Reset */
1675                 buf[1] = 0x04;
1676
1677                 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
1678                         buf[i] = 0xff;
1679                 }
1680
1681                 pc = usbd_xfer_get_frame(xfer, 0);
1682                 usbd_copy_in(pc, 0, &req, sizeof(req));
1683                 pc = usbd_xfer_get_frame(xfer, 1);
1684                 usbd_copy_in(pc, 0, buf, sizeof(buf));
1685
1686                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1687                 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
1688                 usbd_xfer_set_frames(xfer, 2);
1689                 usbd_transfer_submit(xfer);
1690                 break;
1691
1692         default:                        /* Error */
1693                 if (error == USB_ERR_CANCELLED)
1694                         umass_tr_error(xfer, error);
1695                 else
1696                         umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1697                 break;
1698         }
1699 }
1700
1701 static void
1702 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1703 {
1704         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
1705             UMASS_T_CBI_DATA_READ, error);
1706 }
1707
1708 static void
1709 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1710 {
1711         struct umass_softc *sc = usbd_xfer_softc(xfer);
1712
1713         umass_t_cbi_data_clear_stall_callback
1714             (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
1715             sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
1716             UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
1717             UMASS_T_CBI_DATA_WRITE, error);
1718 }
1719
1720 static void
1721 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
1722 {
1723         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
1724             UMASS_T_CBI_STATUS, error);
1725 }
1726
1727 static void
1728 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
1729     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1730 {
1731         struct umass_softc *sc = usbd_xfer_softc(xfer);
1732
1733         switch (USB_GET_STATE(xfer)) {
1734         case USB_ST_TRANSFERRED:
1735 tr_transferred:
1736                 if (next_xfer == UMASS_T_CBI_STATUS) {
1737                         umass_cbi_start_status(sc);
1738                 } else {
1739                         umass_transfer_start(sc, next_xfer);
1740                 }
1741                 break;
1742
1743         case USB_ST_SETUP:
1744                 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1745                         goto tr_transferred;    /* should not happen */
1746                 }
1747                 break;
1748
1749         default:                        /* Error */
1750                 umass_tr_error(xfer, error);
1751                 break;
1752         }
1753 }
1754
1755 static void
1756 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
1757 {
1758         struct umass_softc *sc = usbd_xfer_softc(xfer);
1759         union ccb *ccb = sc->sc_transfer.ccb;
1760         struct usb_device_request req;
1761         struct usb_page_cache *pc;
1762
1763         switch (USB_GET_STATE(xfer)) {
1764         case USB_ST_TRANSFERRED:
1765
1766                 if (sc->sc_transfer.dir == DIR_NONE) {
1767                         umass_cbi_start_status(sc);
1768                 } else {
1769                         umass_transfer_start
1770                             (sc, (sc->sc_transfer.dir == DIR_IN) ?
1771                             UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
1772                 }
1773                 break;
1774
1775         case USB_ST_SETUP:
1776
1777                 if (ccb) {
1778
1779                         /*
1780                          * do a CBI transfer with cmd_len bytes from
1781                          * cmd_data, possibly a data phase of data_len
1782                          * bytes from/to the device and finally a status
1783                          * read phase.
1784                          */
1785
1786                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1787                         req.bRequest = UR_CBI_ADSC;
1788                         USETW(req.wValue, 0);
1789                         req.wIndex[0] = sc->sc_iface_no;
1790                         req.wIndex[1] = 0;
1791                         req.wLength[0] = sc->sc_transfer.cmd_len;
1792                         req.wLength[1] = 0;
1793
1794                         pc = usbd_xfer_get_frame(xfer, 0);
1795                         usbd_copy_in(pc, 0, &req, sizeof(req));
1796                         pc = usbd_xfer_get_frame(xfer, 1);
1797                         usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
1798                             sc->sc_transfer.cmd_len);
1799
1800                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1801                         usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
1802                         usbd_xfer_set_frames(xfer,
1803                             sc->sc_transfer.cmd_len ? 2 : 1);
1804
1805                         DIF(UDMASS_CBI,
1806                             umass_cbi_dump_cmd(sc,
1807                             sc->sc_transfer.cmd_data,
1808                             sc->sc_transfer.cmd_len));
1809
1810                         usbd_transfer_submit(xfer);
1811                 }
1812                 break;
1813
1814         default:                        /* Error */
1815                 /*
1816                  * STALL on the control pipe can be result of the command error.
1817                  * Attempt to clear this STALL same as for bulk pipe also
1818                  * results in command completion interrupt, but ASC/ASCQ there
1819                  * look like not always valid, so don't bother about it.
1820                  */
1821                 if ((error == USB_ERR_STALLED) ||
1822                     (sc->sc_transfer.callback == &umass_cam_cb)) {
1823                         sc->sc_transfer.ccb = NULL;
1824                         (sc->sc_transfer.callback)
1825                             (sc, ccb, sc->sc_transfer.data_len,
1826                             STATUS_CMD_UNKNOWN);
1827                 } else {
1828                         umass_tr_error(xfer, error);
1829                         /* skip reset */
1830                         sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1831                 }
1832                 break;
1833         }
1834 }
1835
1836 static void
1837 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1838 {
1839         struct umass_softc *sc = usbd_xfer_softc(xfer);
1840         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1841         int actlen, sumlen;
1842
1843         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1844
1845         switch (USB_GET_STATE(xfer)) {
1846         case USB_ST_TRANSFERRED:
1847                 sc->sc_transfer.data_rem -= actlen;
1848                 sc->sc_transfer.data_ptr += actlen;
1849                 sc->sc_transfer.actlen += actlen;
1850
1851                 if (actlen < sumlen) {
1852                         /* short transfer */
1853                         sc->sc_transfer.data_rem = 0;
1854                 }
1855         case USB_ST_SETUP:
1856                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1857                     max_bulk, sc->sc_transfer.data_rem);
1858
1859                 if (sc->sc_transfer.data_rem == 0) {
1860                         umass_cbi_start_status(sc);
1861                         break;
1862                 }
1863                 if (max_bulk > sc->sc_transfer.data_rem) {
1864                         max_bulk = sc->sc_transfer.data_rem;
1865                 }
1866                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1867
1868                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1869                     max_bulk);
1870
1871                 usbd_transfer_submit(xfer);
1872                 break;
1873
1874         default:                        /* Error */
1875                 if ((error == USB_ERR_CANCELLED) ||
1876                     (sc->sc_transfer.callback != &umass_cam_cb)) {
1877                         umass_tr_error(xfer, error);
1878                 } else {
1879                         umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
1880                 }
1881                 break;
1882         }
1883 }
1884
1885 static void
1886 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1887 {
1888         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1889             UMASS_T_CBI_DATA_READ, error);
1890 }
1891
1892 static void
1893 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1894 {
1895         struct umass_softc *sc = usbd_xfer_softc(xfer);
1896         uint32_t max_bulk = usbd_xfer_max_len(xfer);
1897         int actlen, sumlen;
1898
1899         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1900
1901         switch (USB_GET_STATE(xfer)) {
1902         case USB_ST_TRANSFERRED:
1903                 sc->sc_transfer.data_rem -= actlen;
1904                 sc->sc_transfer.data_ptr += actlen;
1905                 sc->sc_transfer.actlen += actlen;
1906
1907                 if (actlen < sumlen) {
1908                         /* short transfer */
1909                         sc->sc_transfer.data_rem = 0;
1910                 }
1911         case USB_ST_SETUP:
1912                 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1913                     max_bulk, sc->sc_transfer.data_rem);
1914
1915                 if (sc->sc_transfer.data_rem == 0) {
1916                         umass_cbi_start_status(sc);
1917                         break;
1918                 }
1919                 if (max_bulk > sc->sc_transfer.data_rem) {
1920                         max_bulk = sc->sc_transfer.data_rem;
1921                 }
1922                 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1923
1924                 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1925                     max_bulk);
1926
1927                 usbd_transfer_submit(xfer);
1928                 break;
1929
1930         default:                        /* Error */
1931                 if ((error == USB_ERR_CANCELLED) ||
1932                     (sc->sc_transfer.callback != &umass_cam_cb)) {
1933                         umass_tr_error(xfer, error);
1934                 } else {
1935                         umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
1936                 }
1937                 break;
1938         }
1939 }
1940
1941 static void
1942 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1943 {
1944         umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1945             UMASS_T_CBI_DATA_WRITE, error);
1946 }
1947
1948 static void
1949 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
1950 {
1951         struct umass_softc *sc = usbd_xfer_softc(xfer);
1952         union ccb *ccb = sc->sc_transfer.ccb;
1953         struct usb_page_cache *pc;
1954         uint32_t residue;
1955         uint8_t status;
1956         int actlen;
1957
1958         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1959
1960         switch (USB_GET_STATE(xfer)) {
1961         case USB_ST_TRANSFERRED:
1962
1963                 if (actlen < (int)sizeof(sc->sbl)) {
1964                         goto tr_setup;
1965                 }
1966                 pc = usbd_xfer_get_frame(xfer, 0);
1967                 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
1968
1969                 residue = (sc->sc_transfer.data_len -
1970                     sc->sc_transfer.actlen);
1971
1972                 /* dissect the information in the buffer */
1973
1974                 if (sc->sc_proto & UMASS_PROTO_UFI) {
1975
1976                         /*
1977                          * Section 3.4.3.1.3 specifies that the UFI command
1978                          * protocol returns an ASC and ASCQ in the interrupt
1979                          * data block.
1980                          */
1981
1982                         DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
1983                             "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
1984                             sc->sbl.ufi.ascq);
1985
1986                         status = (((sc->sbl.ufi.asc == 0) &&
1987                             (sc->sbl.ufi.ascq == 0)) ?
1988                             STATUS_CMD_OK : STATUS_CMD_FAILED);
1989
1990                         sc->sc_transfer.ccb = NULL;
1991
1992                         sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1993
1994                         (sc->sc_transfer.callback)
1995                             (sc, ccb, residue, status);
1996
1997                         break;
1998
1999                 } else {
2000
2001                         /* Command Interrupt Data Block */
2002
2003                         DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2004                             sc->sbl.common.type, sc->sbl.common.value);
2005
2006                         if (sc->sbl.common.type == IDB_TYPE_CCI) {
2007
2008                                 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2009
2010                                 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2011                                     (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2012                                     (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2013                                     STATUS_WIRE_FAILED);
2014
2015                                 sc->sc_transfer.ccb = NULL;
2016
2017                                 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2018
2019                                 (sc->sc_transfer.callback)
2020                                     (sc, ccb, residue, status);
2021
2022                                 break;
2023                         }
2024                 }
2025
2026                 /* fallthrough */
2027
2028         case USB_ST_SETUP:
2029 tr_setup:
2030                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2031                 usbd_transfer_submit(xfer);
2032                 break;
2033
2034         default:                        /* Error */
2035                 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2036                     usbd_errstr(error));
2037                 umass_tr_error(xfer, error);
2038                 break;
2039         }
2040 }
2041
2042 /*
2043  * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2044  */
2045
2046 static int
2047 umass_cam_attach_sim(struct umass_softc *sc)
2048 {
2049         struct cam_devq *devq;          /* Per device Queue */
2050
2051         /*
2052          * A HBA is attached to the CAM layer.
2053          *
2054          * The CAM layer will then after a while start probing for devices on
2055          * the bus. The number of SIMs is limited to one.
2056          */
2057
2058         devq = cam_simq_alloc(1 /* maximum openings */ );
2059         if (devq == NULL) {
2060                 return (ENOMEM);
2061         }
2062         sc->sc_sim = cam_sim_alloc
2063             (&umass_cam_action, &umass_cam_poll,
2064             DEVNAME_SIM,
2065             sc /* priv */ ,
2066             sc->sc_unit /* unit number */ ,
2067             &sc->sc_mtx /* mutex */ ,
2068             1 /* maximum device openings */ ,
2069             0 /* maximum tagged device openings */ ,
2070             devq);
2071
2072         if (sc->sc_sim == NULL) {
2073                 cam_simq_free(devq);
2074                 return (ENOMEM);
2075         }
2076
2077         mtx_lock(&sc->sc_mtx);
2078
2079         if (xpt_bus_register(sc->sc_sim, sc->sc_dev,
2080             sc->sc_unit) != CAM_SUCCESS) {
2081                 mtx_unlock(&sc->sc_mtx);
2082                 return (ENOMEM);
2083         }
2084         mtx_unlock(&sc->sc_mtx);
2085
2086         return (0);
2087 }
2088
2089 static void
2090 umass_cam_attach(struct umass_softc *sc)
2091 {
2092 #ifndef USB_DEBUG
2093         if (bootverbose)
2094 #endif
2095                 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2096                     sc->sc_name, cam_sim_path(sc->sc_sim),
2097                     sc->sc_unit, CAM_LUN_WILDCARD,
2098                     cam_sim_path(sc->sc_sim));
2099 }
2100
2101 /* umass_cam_detach
2102  *      detach from the CAM layer
2103  */
2104
2105 static void
2106 umass_cam_detach_sim(struct umass_softc *sc)
2107 {
2108         if (sc->sc_sim != NULL) {
2109                 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2110                         /* accessing the softc is not possible after this */
2111                         sc->sc_sim->softc = UMASS_GONE;
2112                         cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2113                 } else {
2114                         panic("%s: CAM layer is busy\n",
2115                             sc->sc_name);
2116                 }
2117                 sc->sc_sim = NULL;
2118         }
2119 }
2120
2121 /* umass_cam_action
2122  *      CAM requests for action come through here
2123  */
2124
2125 static void
2126 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2127 {
2128         struct umass_softc *sc = (struct umass_softc *)sim->softc;
2129
2130         if (sc == UMASS_GONE ||
2131             (sc != NULL && !usbd_device_attached(sc->sc_udev))) {
2132                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2133                 xpt_done(ccb);
2134                 return;
2135         }
2136         /*
2137          * Verify, depending on the operation to perform, that we either got
2138          * a valid sc, because an existing target was referenced, or
2139          * otherwise the SIM is addressed.
2140          *
2141          * This avoids bombing out at a printf and does give the CAM layer some
2142          * sensible feedback on errors.
2143          */
2144         switch (ccb->ccb_h.func_code) {
2145         case XPT_SCSI_IO:
2146         case XPT_RESET_DEV:
2147         case XPT_GET_TRAN_SETTINGS:
2148         case XPT_SET_TRAN_SETTINGS:
2149         case XPT_CALC_GEOMETRY:
2150                 /* the opcodes requiring a target. These should never occur. */
2151                 if (sc == NULL) {
2152                         DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2153                             "Invalid target (target needed)\n",
2154                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2155                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2156                             ccb->ccb_h.func_code);
2157
2158                         ccb->ccb_h.status = CAM_TID_INVALID;
2159                         xpt_done(ccb);
2160                         goto done;
2161                 }
2162                 break;
2163         case XPT_PATH_INQ:
2164         case XPT_NOOP:
2165                 /*
2166                  * The opcodes sometimes aimed at a target (sc is valid),
2167                  * sometimes aimed at the SIM (sc is invalid and target is
2168                  * CAM_TARGET_WILDCARD)
2169                  */
2170                 if ((sc == NULL) &&
2171                     (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2172                         DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2173                             "Invalid target (no wildcard)\n",
2174                             DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2175                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2176                             ccb->ccb_h.func_code);
2177
2178                         ccb->ccb_h.status = CAM_TID_INVALID;
2179                         xpt_done(ccb);
2180                         goto done;
2181                 }
2182                 break;
2183         default:
2184                 /* XXX Hm, we should check the input parameters */
2185                 break;
2186         }
2187
2188         /* Perform the requested action */
2189         switch (ccb->ccb_h.func_code) {
2190         case XPT_SCSI_IO:
2191                 {
2192                         uint8_t *cmd;
2193                         uint8_t dir;
2194
2195                         if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2196                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2197                         } else {
2198                                 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2199                         }
2200
2201                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2202                             "cmd: 0x%02x, flags: 0x%02x, "
2203                             "%db cmd/%db data/%db sense\n",
2204                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2205                             ccb->ccb_h.target_lun, cmd[0],
2206                             ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2207                             ccb->csio.dxfer_len, ccb->csio.sense_len);
2208
2209                         if (sc->sc_transfer.ccb) {
2210                                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2211                                     "I/O in progress, deferring\n",
2212                                     cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2213                                     ccb->ccb_h.target_lun);
2214                                 ccb->ccb_h.status = CAM_SCSI_BUSY;
2215                                 xpt_done(ccb);
2216                                 goto done;
2217                         }
2218                         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2219                         case CAM_DIR_IN:
2220                                 dir = DIR_IN;
2221                                 break;
2222                         case CAM_DIR_OUT:
2223                                 dir = DIR_OUT;
2224                                 DIF(UDMASS_SCSI,
2225                                     umass_dump_buffer(sc, ccb->csio.data_ptr,
2226                                     ccb->csio.dxfer_len, 48));
2227                                 break;
2228                         default:
2229                                 dir = DIR_NONE;
2230                         }
2231
2232                         ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2233
2234                         /*
2235                          * sc->sc_transform will convert the command to the
2236                          * command format needed by the specific command set
2237                          * and return the converted command in
2238                          * "sc->sc_transfer.cmd_data"
2239                          */
2240                         if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2241
2242                                 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2243                                         const char *pserial;
2244
2245                                         pserial = usb_get_serial(sc->sc_udev);
2246
2247                                         /*
2248                                          * Umass devices don't generally report their serial numbers
2249                                          * in the usual SCSI way.  Emulate it here.
2250                                          */
2251                                         if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2252                                             (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) &&
2253                                             (pserial[0] != '\0')) {
2254                                                 struct scsi_vpd_unit_serial_number *vpd_serial;
2255
2256                                                 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2257                                                 vpd_serial->length = strlen(pserial);
2258                                                 if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2259                                                         vpd_serial->length = sizeof(vpd_serial->serial_num);
2260                                                 memcpy(vpd_serial->serial_num, pserial, vpd_serial->length);
2261                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
2262                                                 ccb->ccb_h.status = CAM_REQ_CMP;
2263                                                 xpt_done(ccb);
2264                                                 goto done;
2265                                         }
2266
2267                                         /*
2268                                          * Handle EVPD inquiry for broken devices first
2269                                          * NO_INQUIRY also implies NO_INQUIRY_EVPD
2270                                          */
2271                                         if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2272                                             (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2273
2274                                                 scsi_set_sense_data(&ccb->csio.sense_data,
2275                                                         /*sense_format*/ SSD_TYPE_NONE,
2276                                                         /*current_error*/ 1,
2277                                                         /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2278                                                         /*asc*/ 0x24,
2279                                                         /*ascq*/ 0x00,
2280                                                         /*extra args*/ SSD_ELEM_NONE);
2281                                                 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2282                                                 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2283                                                     CAM_AUTOSNS_VALID;
2284                                                 xpt_done(ccb);
2285                                                 goto done;
2286                                         }
2287                                         /*
2288                                          * Return fake inquiry data for
2289                                          * broken devices
2290                                          */
2291                                         if (sc->sc_quirks & NO_INQUIRY) {
2292                                                 memcpy(ccb->csio.data_ptr, &fake_inq_data,
2293                                                     sizeof(fake_inq_data));
2294                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
2295                                                 ccb->ccb_h.status = CAM_REQ_CMP;
2296                                                 xpt_done(ccb);
2297                                                 goto done;
2298                                         }
2299                                         if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2300                                                 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2301                                         }
2302                                 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2303                                         if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2304                                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
2305                                                 ccb->ccb_h.status = CAM_REQ_CMP;
2306                                                 xpt_done(ccb);
2307                                                 goto done;
2308                                         }
2309                                 }
2310                                 umass_command_start(sc, dir, ccb->csio.data_ptr,
2311                                     ccb->csio.dxfer_len,
2312                                     ccb->ccb_h.timeout,
2313                                     &umass_cam_cb, ccb);
2314                         }
2315                         break;
2316                 }
2317         case XPT_PATH_INQ:
2318                 {
2319                         struct ccb_pathinq *cpi = &ccb->cpi;
2320
2321                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2322                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2323                             ccb->ccb_h.target_lun);
2324
2325                         /* host specific information */
2326                         cpi->version_num = 1;
2327                         cpi->hba_inquiry = 0;
2328                         cpi->target_sprt = 0;
2329                         cpi->hba_misc = PIM_NO_6_BYTE;
2330                         cpi->hba_eng_cnt = 0;
2331                         cpi->max_target = UMASS_SCSIID_MAX;     /* one target */
2332                         cpi->initiator_id = UMASS_SCSIID_HOST;
2333                         strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2334                         strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2335                         strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2336                         cpi->unit_number = cam_sim_unit(sim);
2337                         cpi->bus_id = sc->sc_unit;
2338                         cpi->protocol = PROTO_SCSI;
2339                         cpi->protocol_version = SCSI_REV_2;
2340                         cpi->transport = XPORT_USB;
2341                         cpi->transport_version = 0;
2342
2343                         if (sc == NULL) {
2344                                 cpi->base_transfer_speed = 0;
2345                                 cpi->max_lun = 0;
2346                         } else {
2347                                 if (sc->sc_quirks & FLOPPY_SPEED) {
2348                                         cpi->base_transfer_speed =
2349                                             UMASS_FLOPPY_TRANSFER_SPEED;
2350                                 } else {
2351                                         switch (usbd_get_speed(sc->sc_udev)) {
2352                                         case USB_SPEED_SUPER:
2353                                                 cpi->base_transfer_speed =
2354                                                     UMASS_SUPER_TRANSFER_SPEED;
2355                                                 cpi->maxio = MAXPHYS;
2356                                                 break;
2357                                         case USB_SPEED_HIGH:
2358                                                 cpi->base_transfer_speed =
2359                                                     UMASS_HIGH_TRANSFER_SPEED;
2360                                                 break;
2361                                         default:
2362                                                 cpi->base_transfer_speed =
2363                                                     UMASS_FULL_TRANSFER_SPEED;
2364                                                 break;
2365                                         }
2366                                 }
2367                                 cpi->max_lun = sc->sc_maxlun;
2368                         }
2369
2370                         cpi->ccb_h.status = CAM_REQ_CMP;
2371                         xpt_done(ccb);
2372                         break;
2373                 }
2374         case XPT_RESET_DEV:
2375                 {
2376                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2377                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2378                             ccb->ccb_h.target_lun);
2379
2380                         umass_reset(sc);
2381
2382                         ccb->ccb_h.status = CAM_REQ_CMP;
2383                         xpt_done(ccb);
2384                         break;
2385                 }
2386         case XPT_GET_TRAN_SETTINGS:
2387                 {
2388                         struct ccb_trans_settings *cts = &ccb->cts;
2389
2390                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2391                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2392                             ccb->ccb_h.target_lun);
2393
2394                         cts->protocol = PROTO_SCSI;
2395                         cts->protocol_version = SCSI_REV_2;
2396                         cts->transport = XPORT_USB;
2397                         cts->transport_version = 0;
2398                         cts->xport_specific.valid = 0;
2399
2400                         ccb->ccb_h.status = CAM_REQ_CMP;
2401                         xpt_done(ccb);
2402                         break;
2403                 }
2404         case XPT_SET_TRAN_SETTINGS:
2405                 {
2406                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2407                             cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2408                             ccb->ccb_h.target_lun);
2409
2410                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2411                         xpt_done(ccb);
2412                         break;
2413                 }
2414         case XPT_CALC_GEOMETRY:
2415                 {
2416                         cam_calc_geometry(&ccb->ccg, /* extended */ 1);
2417                         xpt_done(ccb);
2418                         break;
2419                 }
2420         case XPT_NOOP:
2421                 {
2422                         DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
2423                             sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2424                             ccb->ccb_h.target_lun);
2425
2426                         ccb->ccb_h.status = CAM_REQ_CMP;
2427                         xpt_done(ccb);
2428                         break;
2429                 }
2430         default:
2431                 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
2432                     "Not implemented\n",
2433                     sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2434                     ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2435
2436                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2437                 xpt_done(ccb);
2438                 break;
2439         }
2440
2441 done:
2442         return;
2443 }
2444
2445 static void
2446 umass_cam_poll(struct cam_sim *sim)
2447 {
2448         struct umass_softc *sc = (struct umass_softc *)sim->softc;
2449
2450         if (sc == UMASS_GONE)
2451                 return;
2452
2453         DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
2454
2455         usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
2456 }
2457
2458
2459 /* umass_cam_cb
2460  *      finalise a completed CAM command
2461  */
2462
2463 static void
2464 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2465     uint8_t status)
2466 {
2467         ccb->csio.resid = residue;
2468
2469         switch (status) {
2470         case STATUS_CMD_OK:
2471                 ccb->ccb_h.status = CAM_REQ_CMP;
2472                 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
2473                     (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
2474                     (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
2475                         struct scsi_read_capacity_data *rcap;
2476                         uint32_t maxsector;
2477
2478                         rcap = (void *)(ccb->csio.data_ptr);
2479                         maxsector = scsi_4btoul(rcap->addr) - 1;
2480                         scsi_ulto4b(maxsector, rcap->addr);
2481                 }
2482                 /*
2483                  * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2484                  * of pages supported by the device - otherwise, CAM
2485                  * will never ask us for the serial number if the
2486                  * device cannot handle that by itself.
2487                  */
2488                 if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
2489                     sc->sc_transfer.cmd_data[0] == INQUIRY &&
2490                     (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2491                     sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
2492                     (usb_get_serial(sc->sc_udev)[0] != '\0')) {
2493                         struct ccb_scsiio *csio;
2494                         struct scsi_vpd_supported_page_list *page_list;
2495
2496                         csio = &ccb->csio;
2497                         page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
2498                         if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
2499                                 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
2500                                 page_list->length++;
2501                         }
2502                 }
2503                 xpt_done(ccb);
2504                 break;
2505
2506         case STATUS_CMD_UNKNOWN:
2507         case STATUS_CMD_FAILED:
2508
2509                 /* fetch sense data */
2510
2511                 /* the rest of the command was filled in at attach */
2512                 sc->cam_scsi_sense.length = ccb->csio.sense_len;
2513
2514                 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
2515                     "sense data\n", ccb->csio.sense_len);
2516
2517                 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
2518                     sizeof(sc->cam_scsi_sense))) {
2519
2520                         if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
2521                             (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
2522                                 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
2523                         }
2524                         umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
2525                             ccb->csio.sense_len, ccb->ccb_h.timeout,
2526                             &umass_cam_sense_cb, ccb);
2527                 }
2528                 break;
2529
2530         default:
2531                 /*
2532                  * The wire protocol failed and will hopefully have
2533                  * recovered. We return an error to CAM and let CAM
2534                  * retry the command if necessary.
2535                  */
2536                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2537                 xpt_done(ccb);
2538                 break;
2539         }
2540 }
2541
2542 /*
2543  * Finalise a completed autosense operation
2544  */
2545 static void
2546 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2547     uint8_t status)
2548 {
2549         uint8_t *cmd;
2550
2551         switch (status) {
2552         case STATUS_CMD_OK:
2553         case STATUS_CMD_UNKNOWN:
2554         case STATUS_CMD_FAILED: {
2555                 int key, sense_len;
2556
2557                 ccb->csio.sense_resid = residue;
2558                 sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
2559                 key = scsi_get_sense_key(&ccb->csio.sense_data, sense_len,
2560                                          /*show_errors*/ 1);
2561
2562                 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2563                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2564                 } else {
2565                         cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2566                 }
2567
2568                 /*
2569                  * Getting sense data always succeeds (apart from wire
2570                  * failures):
2571                  */
2572                 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2573                     (cmd[0] == INQUIRY) &&
2574                     (key == SSD_KEY_UNIT_ATTENTION)) {
2575                         /*
2576                          * Ignore unit attention errors in the case where
2577                          * the Unit Attention state is not cleared on
2578                          * REQUEST SENSE. They will appear again at the next
2579                          * command.
2580                          */
2581                         ccb->ccb_h.status = CAM_REQ_CMP;
2582                 } else if (key == SSD_KEY_NO_SENSE) {
2583                         /*
2584                          * No problem after all (in the case of CBI without
2585                          * CCI)
2586                          */
2587                         ccb->ccb_h.status = CAM_REQ_CMP;
2588                 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2589                             (cmd[0] == READ_CAPACITY) &&
2590                     (key == SSD_KEY_UNIT_ATTENTION)) {
2591                         /*
2592                          * Some devices do not clear the unit attention error
2593                          * on request sense. We insert a test unit ready
2594                          * command to make sure we clear the unit attention
2595                          * condition, then allow the retry to proceed as
2596                          * usual.
2597                          */
2598
2599                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2600                             | CAM_AUTOSNS_VALID;
2601                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2602
2603 #if 0
2604                         DELAY(300000);
2605 #endif
2606                         DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
2607                             "TEST_UNIT_READY\n");
2608
2609                         /* the rest of the command was filled in at attach */
2610
2611                         if (umass_std_transform(sc, ccb,
2612                             &sc->cam_scsi_test_unit_ready.opcode,
2613                             sizeof(sc->cam_scsi_test_unit_ready))) {
2614                                 umass_command_start(sc, DIR_NONE, NULL, 0,
2615                                     ccb->ccb_h.timeout,
2616                                     &umass_cam_quirk_cb, ccb);
2617                         }
2618                         break;
2619                 } else {
2620                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2621                             | CAM_AUTOSNS_VALID;
2622                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2623                 }
2624                 xpt_done(ccb);
2625                 break;
2626         }
2627         default:
2628                 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
2629                     "status %d\n", status);
2630                 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2631                 xpt_done(ccb);
2632         }
2633 }
2634
2635 /*
2636  * This completion code just handles the fact that we sent a test-unit-ready
2637  * after having previously failed a READ CAPACITY with CHECK_COND.  Even
2638  * though this command succeeded, we have to tell CAM to retry.
2639  */
2640 static void
2641 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2642     uint8_t status)
2643 {
2644         DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
2645             "returned status %d\n", status);
2646
2647         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2648             | CAM_AUTOSNS_VALID;
2649         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2650         xpt_done(ccb);
2651 }
2652
2653 /*
2654  * SCSI specific functions
2655  */
2656
2657 static uint8_t
2658 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2659     uint8_t cmd_len)
2660 {
2661         if ((cmd_len == 0) ||
2662             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2663                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2664                     "length: %d bytes\n", cmd_len);
2665                 return (0);             /* failure */
2666         }
2667         sc->sc_transfer.cmd_len = cmd_len;
2668
2669         switch (cmd_ptr[0]) {
2670         case TEST_UNIT_READY:
2671                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2672                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2673                             "to START_UNIT\n");
2674                         memset(sc->sc_transfer.cmd_data, 0, cmd_len);
2675                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2676                         sc->sc_transfer.cmd_data[4] = SSS_START;
2677                         return (1);
2678                 }
2679                 break;
2680
2681         case INQUIRY:
2682                 /*
2683                  * some drives wedge when asked for full inquiry
2684                  * information.
2685                  */
2686                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2687                         memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2688                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2689                         return (1);
2690                 }
2691                 break;
2692         }
2693
2694         memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2695         return (1);
2696 }
2697
2698 static uint8_t
2699 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
2700 {
2701         if ((cmd_len == 0) ||
2702             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2703                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2704                     "length: %d bytes\n", cmd_len);
2705                 return (0);             /* failure */
2706         }
2707         switch (cmd_ptr[0]) {
2708                 /* these commands are defined in RBC: */
2709         case READ_10:
2710         case READ_CAPACITY:
2711         case START_STOP_UNIT:
2712         case SYNCHRONIZE_CACHE:
2713         case WRITE_10:
2714         case 0x2f:                      /* VERIFY_10 is absent from
2715                                          * scsi_all.h??? */
2716         case INQUIRY:
2717         case MODE_SELECT_10:
2718         case MODE_SENSE_10:
2719         case TEST_UNIT_READY:
2720         case WRITE_BUFFER:
2721                 /*
2722                  * The following commands are not listed in my copy of the
2723                  * RBC specs. CAM however seems to want those, and at least
2724                  * the Sony DSC device appears to support those as well
2725                  */
2726         case REQUEST_SENSE:
2727         case PREVENT_ALLOW:
2728
2729                 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2730
2731                 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
2732                         memset(sc->sc_transfer.cmd_data + cmd_len,
2733                             0, 12 - cmd_len);
2734                         cmd_len = 12;
2735                 }
2736                 sc->sc_transfer.cmd_len = cmd_len;
2737                 return (1);             /* sucess */
2738
2739                 /* All other commands are not legal in RBC */
2740         default:
2741                 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
2742                     "command 0x%02x\n", cmd_ptr[0]);
2743                 return (0);             /* failure */
2744         }
2745 }
2746
2747 static uint8_t
2748 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2749     uint8_t cmd_len)
2750 {
2751         if ((cmd_len == 0) ||
2752             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2753                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2754                     "length: %d bytes\n", cmd_len);
2755                 return (0);             /* failure */
2756         }
2757         /* An UFI command is always 12 bytes in length */
2758         sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
2759
2760         /* Zero the command data */
2761         memset(sc->sc_transfer.cmd_data, 0, UFI_COMMAND_LENGTH);
2762
2763         switch (cmd_ptr[0]) {
2764                 /*
2765                  * Commands of which the format has been verified. They
2766                  * should work. Copy the command into the (zeroed out)
2767                  * destination buffer.
2768                  */
2769         case TEST_UNIT_READY:
2770                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2771                         /*
2772                          * Some devices do not support this command. Start
2773                          * Stop Unit should give the same results
2774                          */
2775                         DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
2776                             "to START_UNIT\n");
2777
2778                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2779                         sc->sc_transfer.cmd_data[4] = SSS_START;
2780                         return (1);
2781                 }
2782                 break;
2783
2784         case REZERO_UNIT:
2785         case REQUEST_SENSE:
2786         case FORMAT_UNIT:
2787         case INQUIRY:
2788         case START_STOP_UNIT:
2789         case SEND_DIAGNOSTIC:
2790         case PREVENT_ALLOW:
2791         case READ_CAPACITY:
2792         case READ_10:
2793         case WRITE_10:
2794         case POSITION_TO_ELEMENT:       /* SEEK_10 */
2795         case WRITE_AND_VERIFY:
2796         case VERIFY:
2797         case MODE_SELECT_10:
2798         case MODE_SENSE_10:
2799         case READ_12:
2800         case WRITE_12:
2801         case READ_FORMAT_CAPACITIES:
2802                 break;
2803
2804                 /*
2805                  * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2806                  * required for UFI devices, so it is appropriate to fake
2807                  * success.
2808                  */
2809         case SYNCHRONIZE_CACHE:
2810                 return (2);
2811
2812         default:
2813                 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
2814                     "command 0x%02x\n", cmd_ptr[0]);
2815                 return (0);             /* failure */
2816         }
2817
2818         memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2819         return (1);                     /* success */
2820 }
2821
2822 /*
2823  * 8070i (ATAPI) specific functions
2824  */
2825 static uint8_t
2826 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2827     uint8_t cmd_len)
2828 {
2829         if ((cmd_len == 0) ||
2830             (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2831                 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2832                     "length: %d bytes\n", cmd_len);
2833                 return (0);             /* failure */
2834         }
2835         /* An ATAPI command is always 12 bytes in length. */
2836         sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
2837
2838         /* Zero the command data */
2839         memset(sc->sc_transfer.cmd_data, 0, ATAPI_COMMAND_LENGTH);
2840
2841         switch (cmd_ptr[0]) {
2842                 /*
2843                  * Commands of which the format has been verified. They
2844                  * should work. Copy the command into the destination
2845                  * buffer.
2846                  */
2847         case INQUIRY:
2848                 /*
2849                  * some drives wedge when asked for full inquiry
2850                  * information.
2851                  */
2852                 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2853                         memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2854
2855                         sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2856                         return (1);
2857                 }
2858                 break;
2859
2860         case TEST_UNIT_READY:
2861                 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2862                         DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2863                             "to START_UNIT\n");
2864                         sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2865                         sc->sc_transfer.cmd_data[4] = SSS_START;
2866                         return (1);
2867                 }
2868                 break;
2869
2870         case REZERO_UNIT:
2871         case REQUEST_SENSE:
2872         case START_STOP_UNIT:
2873         case SEND_DIAGNOSTIC:
2874         case PREVENT_ALLOW:
2875         case READ_CAPACITY:
2876         case READ_10:
2877         case WRITE_10:
2878         case POSITION_TO_ELEMENT:       /* SEEK_10 */
2879         case SYNCHRONIZE_CACHE:
2880         case MODE_SELECT_10:
2881         case MODE_SENSE_10:
2882         case READ_BUFFER:
2883         case 0x42:                      /* READ_SUBCHANNEL */
2884         case 0x43:                      /* READ_TOC */
2885         case 0x44:                      /* READ_HEADER */
2886         case 0x47:                      /* PLAY_MSF (Play Minute/Second/Frame) */
2887         case 0x48:                      /* PLAY_TRACK */
2888         case 0x49:                      /* PLAY_TRACK_REL */
2889         case 0x4b:                      /* PAUSE */
2890         case 0x51:                      /* READ_DISK_INFO */
2891         case 0x52:                      /* READ_TRACK_INFO */
2892         case 0x54:                      /* SEND_OPC */
2893         case 0x59:                      /* READ_MASTER_CUE */
2894         case 0x5b:                      /* CLOSE_TR_SESSION */
2895         case 0x5c:                      /* READ_BUFFER_CAP */
2896         case 0x5d:                      /* SEND_CUE_SHEET */
2897         case 0xa1:                      /* BLANK */
2898         case 0xa5:                      /* PLAY_12 */
2899         case 0xa6:                      /* EXCHANGE_MEDIUM */
2900         case 0xad:                      /* READ_DVD_STRUCTURE */
2901         case 0xbb:                      /* SET_CD_SPEED */
2902         case 0xe5:                      /* READ_TRACK_INFO_PHILIPS */
2903                 break;
2904
2905         case READ_12:
2906         case WRITE_12:
2907         default:
2908                 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
2909                     "command 0x%02x - trying anyway\n",
2910                     cmd_ptr[0]);
2911                 break;
2912         }
2913
2914         memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2915         return (1);                     /* success */
2916 }
2917
2918 static uint8_t
2919 umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
2920     uint8_t cmdlen)
2921 {
2922         return (0);                     /* failure */
2923 }
2924
2925 static uint8_t
2926 umass_std_transform(struct umass_softc *sc, union ccb *ccb,
2927     uint8_t *cmd, uint8_t cmdlen)
2928 {
2929         uint8_t retval;
2930
2931         retval = (sc->sc_transform) (sc, cmd, cmdlen);
2932
2933         if (retval == 2) {
2934                 ccb->ccb_h.status = CAM_REQ_CMP;
2935                 xpt_done(ccb);
2936                 return (0);
2937         } else if (retval == 0) {
2938                 ccb->ccb_h.status = CAM_REQ_INVALID;
2939                 xpt_done(ccb);
2940                 return (0);
2941         }
2942         /* Command should be executed */
2943         return (1);
2944 }
2945
2946 #ifdef USB_DEBUG
2947 static void
2948 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
2949 {
2950         uint8_t *c = cbw->CBWCDB;
2951
2952         uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
2953         uint32_t tag = UGETDW(cbw->dCBWTag);
2954
2955         uint8_t clen = cbw->bCDBLength;
2956         uint8_t flags = cbw->bCBWFlags;
2957         uint8_t lun = cbw->bCBWLUN;
2958
2959         DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
2960             "(0x%02x%02x%02x%02x%02x%02x%s), "
2961             "data = %db, lun = %d, dir = %s\n",
2962             tag, clen,
2963             c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
2964             dlen, lun, (flags == CBWFLAGS_IN ? "in" :
2965             (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
2966 }
2967
2968 static void
2969 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
2970 {
2971         uint32_t sig = UGETDW(csw->dCSWSignature);
2972         uint32_t tag = UGETDW(csw->dCSWTag);
2973         uint32_t res = UGETDW(csw->dCSWDataResidue);
2974         uint8_t status = csw->bCSWStatus;
2975
2976         DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
2977             "res = %d, status = 0x%02x (%s)\n",
2978             tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
2979             tag, res,
2980             status, (status == CSWSTATUS_GOOD ? "good" :
2981             (status == CSWSTATUS_FAILED ? "failed" :
2982             (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
2983 }
2984
2985 static void
2986 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
2987 {
2988         uint8_t *c = cmd;
2989         uint8_t dir = sc->sc_transfer.dir;
2990
2991         DPRINTF(sc, UDMASS_BBB, "cmd = %db "
2992             "(0x%02x%02x%02x%02x%02x%02x%s), "
2993             "data = %db, dir = %s\n",
2994             cmdlen,
2995             c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
2996             sc->sc_transfer.data_len,
2997             (dir == DIR_IN ? "in" :
2998             (dir == DIR_OUT ? "out" :
2999             (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3000 }
3001
3002 static void
3003 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3004     uint32_t printlen)
3005 {
3006         uint32_t i, j;
3007         char s1[40];
3008         char s2[40];
3009         char s3[5];
3010
3011         s1[0] = '\0';
3012         s3[0] = '\0';
3013
3014         sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3015         for (i = 0; (i < buflen) && (i < printlen); i++) {
3016                 j = i % 16;
3017                 if (j == 0 && i != 0) {
3018                         DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3019                             s1, s2);
3020                         s2[0] = '\0';
3021                 }
3022                 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3023         }
3024         if (buflen > printlen)
3025                 sprintf(s3, " ...");
3026         DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3027             s1, s2, s3);
3028 }
3029
3030 #endif