source: rtems-libbsd/freebsd/sys/dev/usb/storage/umass.c @ 3d1e767

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d1e767 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

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